Java Inheritance

  1. Single Inheritance in Java
  2. Multi-level Inheritance in Java
  3. Multiple Inheritance in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Single Inheritance in Java


Inherit_Single.java

public class Inherit_Single {
	
	protected String str;
	
	Inherit_Single() {
		
		str = "Java ";
	}
}
 
class SubClass extends Inherit_Single {
	
	SubClass() {
	
		str = str.concat("World !!!");
	}
	
	void display()
	{
		
		System.out.println(str);
	}
}
 
class MainClass {
	
	public static void main (String args[]){
		
		SubClass obj = new SubClass();
		
		obj.display();
	}
}

Sample Output

Java World !!!




B. Multi-level Inheritance in Java


Inherit_Multilevel.java

public class Inherit_Multilevel {
 
	protected String str;
	
	Inherit_Multilevel() {
		
		str = "J";
	}
}
 
class SubClass1 extends Inherit_Multilevel {
	
	SubClass1() {
		
		str = str.concat("A");
	}
}
 
class SubClass2 extends SubClass1 {
	
	SubClass2() {
		
		str = str.concat("V");
	}
}
 
class SubClass3 extends SubClass2 {
	
	SubClass3() {
		
		str = str.concat("A");
	}
	
	void display() {
		
		System.out.println(str);
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		SubClass3 obj = new SubClass3();
		
		obj.display();
	}
}

Sample Output

JAVA




C. Multiple Inheritance in Java


Inherit_Multiple.java

public interface Inherit_Multiple {
	
	String str1 = "This ";
}
 
interface Interface2 {
	
	String str2 = "is ";
}
 
interface Interface3 {
	
	String str3 = "Java ";
}
 
interface Interface4 {
	
	String str4 = "World !!!"; 
}
 
class SubClass implements Inherit_Multiple, Interface2, Interface3, Interface4 {
	
	String str;
	
	SubClass() {
		
		str = str1.concat(str2).concat(str3).concat(str4);
	}
	
	void display() {
		
		System.out.println(str);
	}
}

class MainClass {
	
	public static void main(String args[]) {
		
		SubClass obj = new SubClass();
		
		obj.display();
	}
}

Sample Output

This is Java World !!!






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com