Inheritance

Different kinds of objects often have a certain amount in common with each other. Employee, boss, policeman, farmer share the characteristics of Human such as gender, age, name…

Object-oriented Programming (OOP) allows classes to inherit commonly used state and behavior from other classes. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:

Human class

In this example, Human is the superclass of Employee class. I simplify the human class; so you can be easier to understand:

Employee Class

This gives Employee all the same fields and methods as Human class, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. Superclass’s code will not appear in the source file of each subclass; but they’re accessible within the subclass.

Run The Application

Right click to the Employee class; select Run As -> Java Application.

Output

As you can see, the attributes gender, age & name do not reside in Employee class; but they are still accessible.