Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications. By using inheritance we can create a general class that defines behavior common to set of related items. This class can be inherited by other. In common terminology we can call this as reusability. It's always good that we can reuse something that already existing rather that creating the same all over again. Reusing the resources which already exist reduces the work and memory wastage and we can add some more functionality to that resource.
Inheritance is a compile-time mechanism in Java that allows you to extend a class (called the baseclass or superclass) with another class (called the derived class or subclass). inheritance relationships in Java by extending a class. It's also important to understand that the two most common reasons to use inheritance are
" To promote code reuse
" To use polymorphism
Inheritance is a way of acquiring the properties of one class into another class. That is establishing the super class and sub class relationship. The superclass is a class whose properties are inherited by a subclass and the superclass always implements the generic behavior. The subclass is a class whose properties are inherited from superclass and redefined in its own nature. Let's start with reuse. A common design approach is to create a generic version of a class with the intention of creating more specialized subclasses that inherit from it.
Interface defines is-a relationship is-a between superclass and subclass. This means the object of a subclass can be used wherever object of the superclass can be used. But Java won't support multiple inheritance.
//Person is more generic class / Superclass
public void dispDetails()
//Emloyee class which is derived from Person
class Employee extends Person
public void dispEmp()
// main class
public class Test
public static void main(String args)
Employee emp=new Employee();
emp.dispDetails(); //Displays Person Details
emp.dispEmp(); //Displays Employee details
Output of the program is as follows:
PRINT NAME: Venu Gopal Darur
PRINT NUMBER: 0
PRINT ONE 1