Rajinder Menu

Inheritance in Java




Inheritance means acquiring properties from parent by child. The same concept applies to classes in java.

In java one class can inherit properties from another class. The class which is inheriting the properties is called subclass and the class from which it is inheriting properties is called as superclass. This is done using extends keyword. For example


class Parent{
...
}

public class Child extends Parent{
...

}

Here class Child is extending or inheriting the class Parent using extends keyword. Child is called as subclass or derived class and Parent is called as superclass.

The properties subclass is inheriting are the superclass members- fields and methods.

This is also known as class-based inheritance.

Consider following example:

class Parent{

  int age;
  String name;
  public void print() {
    System.out.println("Age is:"+age);
    System.out.println("Name is:"+name);
  }

}
public class Child extends Parent{

 public static void main(String[] args) {

  Child childObject= new Child();
  childObject.age=10;
  childObject.name="James";
  childObject.print();
 }

}


Output:
Age is:10
Name is:James


In above code class Parent is superclass and class Child is subclass. Parent has three members namely age, name and a method print(). Child is inheriting these members from the superclass Parent. Therefore all these members are also present in the Child class though we have not provided or defined these members in Child class. Still Child class is able to use these members. This is due to inheritance.

Despite that subclass inherits properties from superclass, subclass can provide its own members also. For example:

public class Child extends Parent{

  //subclass own member
  String childSchool;

  //subclass own member
  public void newPrint() {

    //’age’ inherited from superclass
    System.out.println("Age is:"+age);
   
   //’name’ inherited from superclass
   System.out.println("Name is:"+name);

   //subclass member
   System.out.println("Child School is:"+childSchool);
  }

 public static void main(String[] args) {

  Child childObject= new Child();
  childObject.age=10;
  childObject.name="James";
  childObject.childSchool="SchoolABC";
  childObject.newPrint();
 }

}


Output:

Age is:10
Name is:James
Child School is:SchoolABC

In above code the subclass Child has provided its own members which are specific to it namely variable childSchool and a member function newPrint(). These members are present in Child subclass in addition to the inherited members from Parent superclass.

In Java Object class is superclass of all classes.


Multilevel Inheritance


Consider following inheritance hierarchy:



class A{
...
}

class B extends A{
...
}

class C extends B{
...
}



There is a class A which is extended by class B i.e. A is superclass and B is subclass and there is another class C which extends B i.e. here B is superclass for C and C is subclass for both B and A then this is known as multilevel inheritance. This can go further more like some another class D extending C. This is not just limited to 3 levels as:


class D extends C{
...

}

B inherits members of A class. C inherits members of A and B class. D inherits members of A, B and C classes.







Multiple Inheritance


Multiple inheritance means a class extending more than one class. For example


class A{
...
}

class B {
...
}

class C extends A, B{
...
}


Here in above code C is extending more than one superclasses A and B.

Java does not support the concept of multiple inheritance. So the above is not valid in Java.

In Java a class can extend only one superclass. Though the multiple inheritance in Java can be implemented using interfaces but not through classes.

I would like to know your comments and if you liked the article then please share it on social networking buttons.


No comments:

Post a Comment