☰ Topics

Inheritance

Using inheritance, we can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it. The class that is inherited is called super class or parent class and the class that does the inheriting is called subclass. Thus, a subclass is a speciliazed version of a superclass which inherits all the instance variables and method defined by the superclass and its own unique elements. We can say that, inheritance refers to the sharing of characteristics from parent to child.

Advantages:-

  • It reduces redundancy i.e. sharing of data.
  • Maintainability becomes easier.
  • Dynammic execution can be done.

Disadvantage: Complexity is increased if there are many levels.


 


Types of inheritance: In java, inheritence is only hierarichical which includes single and multilevel. Java doesn't supports multiple inheritance.

Note:- Private members of super class can't be inherited to sub class i.e. it is not accessible to sub class.

The child class is inherited from parent class using extends keyword. We can define both parent and child class either in the same file or in the different file, we have to save the filename with the class name having main() defined in it i.e. the lowest child class. Byte code is generated for both parent and child class seperately. There is no need to compile parent class. Its byte code is generated automatically.

Syntax:-
class <parent-classname>
{
    //body
}
class <child-classname> extends <parent-classname>
{
    //body
}
                        

class parent            //parent class
{
	int i=1,j=2;
	void show()
	{
		System.out.println("i="+i);
		System.out.println("j="+j);
	}
}
class child extends parent        //child class
{
	int k=3;
	void showk()
	{
		System.out.println("k="+k);
	}
	void sum()
	{
		int s;
		s=i+j+k;
		System.out.println("s="+s);
	}
	public static void main(String[] arg)
	{
		parent p=new parent();
		child c=new child();
		p.show();
		c.show();      //object of child class can access the members of parent class
		c.showk();
		c.sum();
		//p.sum(); this is not allowed i.e. members of sub class can't be accessed using object of super class
	}
}
                            

OUTPUT:

E:\java\new_java>javac child.java

E:\java\new_java>java child
i=1
j=2
i=1
j=2
k=3
s=6
                        

super keyword:-

Whenever a subclass needs to refer to its immediate superclass, it can do so by the use of keyword super. super has two general forms. The first calls the super class' constructor i.e. a subclass can call a constructor method defined by its super class by use of the super keyword. super must be the first statement executed inside the subclass constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass.

Example of super keyword when used as constructor:-
class first            //parent class
{
	int i,j,k;
	first(int a,int b,int c)
	{
		i=a;
		j=b;
		k=c;
		System.out.println(i+" "+j+" "+k);
	}
	first(int x)
	{
		i=j=k=x;
		System.out.println(i+" "+j+" "+k);
	}
}
class second extends first       //child class
{
	int l;
	second(int a,int b,int c,int d)
	{
		super(a,b,c);
		l=d;
		System.out.println(l);
	}
	second(int x)
	{
		super(x);
		l=x;
		System.out.println(l);
	}
	public static void main(String[] arg)
	{
		first f1=new first(5);
		first f2=new first(2,4,6);
		second s1=new second(10);
		second s2=new second(2,4,6,10);
	}
}
                            

OUTPUT:

E:\java\new_java>javac second.java

E:\java\new_java>java second
5 5 5
2 4 6
10 10 10
10
2 4 6
10
                        

Whenever the constructor of subclass is called, the constructor of super class is called automatically even when super keyword is not used but in that case empty constructor is called but the constructor of super class may have parameters, therefore, it is better to use super keyword.

Example:-
class A
{
	A()
	{
		System.out.print("inside A");
	}
}
class B extends A
{
	B()
	{
		System.out.print("inside B");
	}
	public static void main(String[] arg)
	{
		B b=new B();
	}
}
                            

OUTPUT:

E:\java\new_java>javac B.java

E:\java\new_java>java B
inside Ainside B
                        


Second use of super:-

The second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the super class. We can say that in case of Method Overriding. It refers to the phenomenon in which signature of function is same but definition is different. It is implemented in atlest two classes when inheritance is involved. It supports late binding.

Example:
class parent1       //parent class
{
	int i=1;
  	public void print()
  	{
    		System.out.println("Welcome");
  	}
}
class child1 extends parent1     //child class
{
	int i=2;
  	public void print()     //overridden method
  	{
		super.print();    //print() of super class gets called
    		System.out.println("to campusedulive.com");
		System.out.println(i);    //i of child class gets printed
		System.out.println(super.i);    //i of parent class gets printed
  	}
  	public static void main(String[] arg)
  	{
    		child1 ob=new child1();
    		ob.print();      //print() of child class gets called
  	}
}
                            

OUTPUT:

E:\java\java>javac child1.java

E:\java\java>java child1
Welcome
to campusedulive.com
2
1