Introduction
In Java, it is possible to inherit member data and member methods from one class to another class. The class derived is called a subclass or a child class. The class from which member data and member methods are inherited is called a superclass or a parent class. The relationship between a superclass and a subclass is called a parent-child relationship.
Inheritance allows you to reuse data and methods available from the superclass to build a new subclass. The use of inheritance is very important to avoid duplication of data and methods (reusability).
Example
In a university there are (of course) students. Let us limit a student’s basic information to a first name (variable firstName) and a last name (variable lastName). Students are divided into bachelor students and master students. Let us assume that the registration fee (variable registrationFee) for a bachelor student is € 1000 and for a master student € 2000. For a master student we also have to register the title of the master thesis (variable thesisTitle).
All students have a first and a last name. This information can be stored in a superclass Student. Also the method inputName() to input these data elements can be part of the Student class.
The registration fee (variable registrationFee) is different for bachelor and master students. Both the bachelor class BachelorStudent and the master class MasterStudent will need this variable to store the correct registration fee. Both classes will be subclasses of the class Student since both classes will use the student names available in the superclass Student. It is easy to understand that the title of the thesis (variable thesisTitle) must be stored in the class MasterStudent.

Class Student
As can be seen in the drawing above, the super class Student will store firstName and lastName as member data of the class. In the class we need a member method inputName() to enter the first and last name of a student.
//Student Class //Carlos De Backer import java.util.Scanner; public class Student{ protected String firstName; protected String lastName; protected void inputName(){ Scanner myKeyboard = new Scanner(System.in); System.out.println("Enter first name"); firstName = myKeyboard.next(); System.out.println("Enter last name"); lastName = myKeyboard.next(); } }
This program must seem familiar to everyone. Only the keyword protected need some more explanation. A protected data member or a protected method member is only available within its class or from a subclass. So, the variables firstName, lastName and the method inputName() can only be called from the class Student or from one of the subclasses BachelorStudent or MasterStudent.
A protected member variable or member method can only be accessed from within its class or from a subclass.
Class BachelorStudent
The class BachelorStudent only stores the registration fee (variable registrationFee). In the next program this variable is defined private. A private variable can only be accessed from within the class.
//BachelorStudent Class //Carlos De Backer public class BachelorStudent extends Student{ private int registrationFee = 1000; protected int getRegistrationFee(){ return registrationFee; } }
The extends keyword in the class definition refers BachelorStudent to the superclass Student. This is the syntax to define a subclass:
class name-of-subclass extends name-of-superclass{ ... }
The protected method getRegistrationFee() will be used to access the private data member registrationFee.
Class MasterStudent
The class MasterStudent stores the registration fee (variable registrationFee) and the title of the master thesis (variable thesisTitle).
//MasterStudent Class //Carlos De Backer import java.util.Scanner; public class MasterStudent extends Student{ protected int registrationFee = 2000; private String thesisTitle; protected void inputThesisTitle(){ Scanner myKeyboard = new Scanner(System.in); System.out.println("Enter title"); thesisTitle = myKeyboard.next(); } }
We defined registrationFee as a protected variable. As you know, this variable is now directly accessible from the class.
Class InheritanceExample
The main() method is available in a class named InheritanceExample.
//InheritanceExample Class //Carlos De Backer public class InheritanceExample{ public static void main(String[] args){ BachelorStudent student1 = new BachelorStudent(); student1.inputName(); System.out.println(student1.firstName + " " + student1.lastName +" " + student1.getRegistrationFee()); MasterStudent student2 = new MasterStudent(); student2.inputName(); student2.inputThesisTitle(); System.out.println(student2.firstName + " " + student2.lastName+" " + student2.registrationFee); } }
Enter first name John Enter last name Davis John Davis 1000 Enter first name Laura Enter last name Rogers Enter title Java Laura Rogers 2000
The InheritanceExample class is easy to understand. Object student1 gets the registration fee from the protected getRegistration() method. Object student2 accesses directly the protected registrationFee variable in the class.