Benefit of using inheritance:
Inheritance allows access of properties and methods of super class by a sub class. Extend keyword is used to inherit all the properties of the superclasses by subclasses. (Subclass is a class derived from another class)
There are two types of inheritance in Java:
Single inheritance or One-level inheritance is when a subclass is derived from parent class. There is only a sub class and it's parent class.
Multiple Inheritance is when a subclass is derived from a derived class. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above (parent) class.
Multilevel inheritance can go up to any number of level.
* Though Java does not support Multiple Inheritance it can be achieved by using the interface and implementing more than one interfaces in a class.
Example of Inheritance in Java:
<title>How to Use Inheritance in the jsp</title>
<h1>How to Use Inheritance in the jsp</h1>
int length =10;
int breadth =20 ;
public int areaOfRectangle() throws java.io.IOException
pw.println("Calculating the area of a rectangle...<br>");
class Square extends Rectangle
public int areaOfSquare() throws java.io.IOException
pw.println("Calculating the area of a square...<br>");
pw = out;
Square sq = new Square();
int area1 = sq.areaOfRectangle();
int area2 = sq.areaOfSquare();
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial
Discuss: Inheritance in Java
Post your Comment