Show Mobile Navigation

Webring

Powered by WebRing.
,

Inheritance in case of classes in Java.

Sachin R Kukale - 02:54

In this section we will see the important topic of inheritance. Inheritance is regarded as the
key feature of any Object Oriented programming langauge. If any language does not support the
concept then it will not be called as true object oriented programming language.
So lets see what does inheritance mean in the context of Java?
In the context of Java the meaning of inheritance is not changed; it retains its real meaning.
In java inheritnace means you can inherit or assume the proeprties of parent class in child class,
in a very convinient way.
In java there are two two aspects to inheritance and these are:

  1. Inheritance in case of classes.
  2. Inheritance in case of interfaces.
Here in this section we will see only the inheritance in case of classes in Java.
Rules to be remember in case of inheritance with classes in Java:
  1. You must use 'extends' keyword to extend the any class.
  2. 'extends' can be used to inherit only classes not interfaces.
  3. Java does not allow multiple inheritance i.e. you cannot inherit more than one classes by using 'extends' keyword.
  4. You can not inherit interfaces using the 'extends' keyword.
  5. If a class A inherits class B then class is called child class and class B is called parent class.
  6. When a child class inherits parent class it assumes all the variables and methods declared by itself. That is you can use the parent class members in child class without again declaring and defining them in child class, unless those members are declared as private.
  7. Even if child class inherits parent class it cannot inherit/assume the private members of child class.
  8. Whenever you create the instance of child class the constructor of parent class automatically gets called before the constructor of child class.
The following code will show the various scenarios in case of inheritance in Java with classes:

public class InheritanceDemo{
public InheritanceDemo(){
System.out.println("Inheritance class constructor.");
}
public void parentMethod(){
//Parent class method.
System.out.println("Method in parent class.");
}
public static void main (String args[]) {
System.out.println("\nCreating the instace of InheritanceDemo class and calling a method.\n");
System.out.println("Only one constructor will be called i.e. InheritanceClass\n");
InheritanceDemo id=new InheritanceDemo();
id.parentMethod();
System.out.println("\nCreating the instace of Child1 class and calling two methods one from parent class and one from child class\n");
System.out.println("Two constructors will be called one of InheritanceDemo and one of Child1 class");
Child1 ch1=new Child1();
ch1.child1Method();
ch1.parentMethod();
//ch1.child2Method();
//You can not call the method of child class from subclass directly.
System.out.println("\nCreating the instance of Child2 class and calling three methods from up the inheritance tree.\n");
System.out.println("Three constructors will be called up to the inheritance tree...");
Child2 ch2=new Child2();
ch2.child2Method();
ch2.child1Method();
ch2.parentMethod();
}
}
class Child1 extends InheritanceDemo{
public Child1(){
//Constructor of child1 class.
System.out.println("Constructor of Child1 class.");
}
public void child1Method(){
System.out.println("Method in child1 class");
}
}
class Child2 extends Child1{
public Child2(){
//Constructor of child2 class.
System.out.println("Constructor of Child1 class.");
}
public void child2Method(){
//Method in child2 class.
System.out.println("Method in child1 class");
}
}

In above example class Child1 inherits class InheritanceDemo and Child2 inherits class Child1. So indirectly Child2 class inherits the InheritnaceDemo class. This inheritance heirarchy is noticed and remember and used by compiler while compiling and running this class. So Child2 is also child class of InheritanceDemo class. But it is not multiple inheritnace. Multiple inheritance means inheriting more than one classes siamalteneously. Remember that it is the case of multilevel inheritnace and Java supports multilevel inheritnace. Becuase we are inheriting the classes in heirarchically, not directly. So in this way you can achieve multiple inherintace to some extent. Now members of class InheritanceDemo are also assumed by Child2 class along with class Child1. So when we create the instance of Child2 class all the constructors up to top most level of inheritance tree will be called i.e. constructors of class InheritanceDemo, Child1 and child2 will be called if we only create the instance of Child2 class.

0 comments:

Post a Comment