What is Inheritance?
---------------------------------A class inherits state and behavior from its super class. Inheritance provides a powerful and natural mechanism for organizing and structuring software programs. Benefit of inheritance is that subclasses provide specialized behaviors from the basis of common elements provided by the super class. Through the use of inheritance, programmers can reuse the code in the super class many times.
Inheritance extends the concept of
Classes and
Objects more to facilitate representation of types based on their similarities. Java uses the keywords
extends and
implements for applying inheritance. In the example the Class Tool is
super class to class
ScrewDriver and
Saws. The inheriting classes acquire the properties of the super class.
java code
Class Tool{
String color;
float price;
}
Class ScrewDriver extends Tool {
String handleType;
String tipType;
}
Class Saws extends Tool {
String bladeSize;
}
Read more
More Inheritance Examples