Single Inheritance Example Java Program

Definition

Single inheritance enables a derived class to inherit properties and behavior from a single parent class. It allows a derived class to inherit the properties and behavior of a base class, thus enabling code reusability as well as adding new features to the existing code. This makes the code much more elegant and less repetitive.

Syntax

class A{
	//Do something
}
class B extends A{
	//Do something
}
class C extends A{
	//Do something
}

Single Inheritance Example Program

class SingleInheritance{
	static int num1=10;
	static int num2=5;
}

class MainInheritance extends SingleInheritance{
	public static void main(String[] args){
	int num3=2;
	int result=num1+num2+num3;
	System.out.println("Result of child class is "+result);
	}
}

Sample Output

Output is:
Result of child class is 17