Continuing from previous topic : Inheritance

How do you achieve inherit data members in Java?

using extends keyword for class & interface.

Example: 
Class A {
 // data variables
 // data methods
}

Class B extends A {
 // data variables
 // data methods
}

interface InterfaceA {
      // Components of Interface
}

interface InterfaceB  extends InterfaceA {
      // Components of Interface
}

What are the rules for Inheritance in Java. ?

 1. Inheritance can be achieved using interface and class.
 2. A class can extend class only similarly interface can extend interface only.
 3. In Java only Single Level, Multilevel & Hierarchical Inheritance can be achieved using extends keyword.
 4. In Java, Multiple Inheritance can be achieved only by implementing multiple interface. 

Next Topic:   How to implement multiple interface in Java?

Advertisements