DEFINITION OF INHERITANCE :
Inheritance is one of the cornerstones of Object Oriented Programming (OOP) which
means to inherit the properties of
one class and extends according to the need. The two keywords used in
inheritance are extends (for class) and
implements (for interface).
Inheritance allows the creation of hierarchical
classifications. In inheritance a general class is used to define the common
methods and variables that can be used in other class. The class which inherits
the general class will get all the public methods and variables present in the
general class. Now, general class is called super class or base class
and the class which inherits its properties is knows as sub class or child class.
To understand the concept of inheritance, consider
an example :
Suppose you want to calculate the area of a square,
a rectangle, a circle having same radius as length of square than by using the
same length available in the base class Shape you can easily extends that class
to Square class or rectangle class or circle class by the use of extends keyword.
class Shape
{
//define a length which is public and can be used in square,
//rectangle and circle classes
public int length = 10;
}
//inherit the properties of Shape class with the use of extends keyword
class Square extends Shape
{
void area()
{
//calculate area of square
int area = length * length;
//print it on the screen
System.out.println("Area of square : "+area);
}
}
class Rectangle extends Shape
{
void area()
{
//define a breadth
int breadth = 7;
//calculate area of rectangle
int area = length * breadth;
//print on the screen
System.out.println("Area of rectangle : "+area);
}
}
class Circle extends Shape
{
void area()
{
//calculate area of circle using length of the shape class as radius
float area = 3.14f * length * length;
//print on the screen
System.out.println("Area of circle : "+area);
}
}
//make a entry class which contains main method
class InheritanceDemo
{
public static void main(String[] args)
{
//object of child class square
Square sq = new Square();
//object of child class rectangle
Rectangle rec = new Rectangle();
//object of child class circle
Circle cir = new Circle();
//call the area methods of all child class to
//get the area of different objects
sq.area();
rec.area();
cir.area();
}
}
Output :
Why we use Inheritance?
Inheritance is used because it provides the Reusability of the code like in the
above example we used the same length from the Shape class thrice in different
three classes.
Types of Inheritance in Java :
There are three general types of inheritance in
java. These are :
1. Simple Inheritance
: In simple inheritance there is one base class and a single child class which
extends the properties of base class.
2. Multilevel Inheritance :
In multilevel inheritance there is single parent and a child class and a
further sub child class which inherit properties of child class.
Hierarchical
Inheritance : In this type of inheritance many child
classes inherit the properties of the same child class like the example used in
this blog post.
Note : Multiple Inheritance in which a class
inherits properties of more than one class is not present in Java to remove the
ambiguity in the code.
System Programming
ReplyDeleteThanks for your ideas. You can also find the details on Affity Solutions, at the System Programming. The main object of the Affity Solutions is to provide quality web services and best training of software languages
thank for you useful infrormation and excellent blog in java
ReplyDeletebest online trainings in java
Good material and very helpful for everyone who’s want to self learn. Types of Inheritance in Java
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteWell researched concepts..Thanks for sharing..java training in chennai
ReplyDeleteIt is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai |
ReplyDeleteInheritance is a part of the oops concept and it is a very important factor in programming. Thanks a lot for sharing such nice information. Keep up the good work going on.
ReplyDeleteInheritance is used because it provides the Reusability of the code like in the above example we used the same length from the Shape class thrice in different three classes.perfect explanation about inheritance .its very useful.thanks for your valuable information.java training in chennai | top 10 java training institutes in chennai
ReplyDeleteI was doing an online research on How to Write a Personal Experience Essay Introduction before I landed on this amazing post and I have learned a lot from the shared article. I wonder where I could have found such information if it was not posted on this site therefore thanks the author for sharing the article with us. I was doing an online research on How to Write a Personal Experience Essay Introduction before I landed on this amazing post and I have learned a lot from the shared article. I wonder where I could have found such information if it was not posted on this site therefore thanks the author for sharing the article with us.
ReplyDeleteThank u for Sharing a good and useful information.
ReplyDeleteHadoop Training in Hyderabad
Java Training in Hyderabad
Thank you so much for useful article about Java Inheritance .
ReplyDeleteThank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
ReplyDeletestudent internships in banjarahills
internship programs in madhapur
nice article.thank you
ReplyDeletejavascodegeeks.net
It is very good and very informative. There is a useful information in it.Thanks for posting..
ReplyDeletehttps://www.apponix.com/Java-Institute/Java-Training-Institute-in-Bangalore.html
I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
ReplyDeleteJava Training in Bangalore
Hello, i think that i saw you visited my weblog thus i came to “return the favor”.I'm trying to find things to improve my site!I suppose its ok to use a few of your ideas!! mac neukölln berlin
ReplyDeleteGreat Tutorial. I also learning from Programiz
ReplyDeleteThis a informative blog, Inheritance is a concept of java programming language. Thanks for sharing this topic with us. If anyone interested to learn more about java, they can join an institute for java course. There are many java course service providers which provide best java course in Kanpur, Mumbai, Delhi, Pune, and other cities in India.
ReplyDeleteYour blog content is so knowledgeable and easily understandable. Keep on posting blog like this. If anyone interested to make career on Pyhton, There are many IT courses service providers which provide best Python Course with Job Placement in Jaipur, Kanpur, Mumbai, Delhi, Pune, and other cities in India.
ReplyDeleteexcellent information. Full Stack Classes In Pune>
ReplyDelete