In programming we always try to write short code and avoid repetition of same code. Inheritance is useful in this work very much. Just like real life, we can create a family hierarchy in programming just like 4-wheeler -> car -> farari. Each child class ( called sub class) inherits all public and protected property (variables, methods, etc.). For example we have two classes one is Father and another is Child.
- package flowkl;
-
- class Father
- {
- // father's public property
- public int father_int = 12;
-
- // father's protected (inheritable property)
- protected void who_i_am()
- {
- System.out.println("I am Father");
- }
-
- // father's inheritable property
- protected void father_property()
- {
- System.out.println("this is father's property");
- }
- }
-
- public class Child extends Father
- {
- // modifying father's property
- public void who_i_am()
- {
- System.out.println("I am Child");
- }
-
- // accessing father's property using super keyword
- public void access_father()
- {
- System.out.println("father variable is "+super.father_int);
- }
-
- // child is adding its own property
- public void child_property()
- {
- System.out.println("It is child's property");
- }
- public static void main(String args[])
- {
- Child object = new Child();
-
- // child object using its own method
- object.who_i_am();
- object.child_property();
-
- // child object using father's method
- object.father_property();
-
- // using father's variable
- System.out.println(object.father_int);
- object.access_father();
- }
- }
Output:
- I am Child
- It is child's property
- this is father's property
- 12
- father variable is 12
Now we have two classes. Look at the syntax of Child class. It is extending Father class and inheriting all its public and protected properties. Properties of inheritance is:
- Child (sub) class inherits all public and protected instances of the parent (super) class.
- Child class can use these instances directly without defining them using super keyword.
- Child class object has modified instances (in child class) and remaining parent class instances. For example in main method, object has who_i_am() method from from Child class and father_property() method from Father class. In other words, we can say that every property (except private) of father is also property of child and child can modify this property. Child can also add its own property (Child class has its own method called child_property()).
- Child can not low the visibility of the parent's instances. Visibility sequence is public > protected > private. So, parents public instances can only be public even in child class. Parent's protected instances can be public or protected (depends upon modification) in the child class. Child class can not access parent's private instances.
- Inheritance is one way. We can not make any cycle like this a=> b=> c=> a or a=> b => c=> b.
- Java allows single inheritance. One class can not inherit from more than one class. In other words, there can be only one father of one child.
Overriding:
When we overload the methods of the super class in sub class then it is called overriding.
- package flowkl;
-
- class Father
- {
- protected void method()
- {
- System.out.println("this is parent function");
- }
- }
-
- public class Child extends Father
- {
- //method overriding by changing parameter
- // we can also change return type along with parameter(s)
- protected int method(int a)
- {
- return a;
- }
-
- public static void main(String args[])
- {
- Child object = new Child();
- System.out.println(object.method(47));
- }
- }
Multiple level Inheritance:
Java allow hierarchy. For example: Child is inheriting Father which is inheriting Grandfather. It means that Child also inheriting from Grandfather indirectly because every non-private property of Grandfather is property of Father and all non-private property of Father is property of Child.
- package flowkl;
-
- class Grandfather
- {
- protected void method()
- {
- System.out.println("method in grandfather");
- }
-
- protected void method2()
- {
- System.out.println("method2 in grandfather");
- }
- }
- class Father extends Grandfather
- {
- protected void method2()
- {
- System.out.println("method2 in father");
- }
- }
-
- public class Child extends Father
- {
- public static void main(String args[])
- {
- Child object = new Child();
- // method call from grandfather
- object.method();
- // method call from father
- object.method2();
- }
- }
OUTPUT:
- method in grandfather
- method2 in father