Introduction To Java Inheritance

By Kunal Kapoor
19-04-2018
449

Inheritance in java is an important feature in Object-oriented programming. It enables hierarchal classifications of various classes and data variables.

It also enables/restricts use of classes and objects within various places in the code


eg


1. myClass.java:

package inheritance_test;

public class myClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		kunal kk=new kunal();
		kapoor kp=new kapoor();
		kp.display_all();
	}

}

2. kunal.java:
package inheritance_test;

public class kunal {	
	
	public int i;
	public int j;
	public int k;
	
	public kunal() {
		this.i=10;
		this.j=20;
		this.k=30;
	}
}

 class kapoor extends kunal{
	
	 public void display_all() {
		 System.out.println("value of i from super class = " + i);
		 System.out.println("value of j from super class = " + j);
		 System.out.println("value of k from super class = " + k);
	 }
	 
}

output:
value of i from super class = 10
value of j from super class = 20
value of k from super class = 30



Related Tutorials