< Browse > Home / Java / Blog article: Java Tutorial : Inheritance In Java – Part 1

| Mobile | RSS

Java Tutorial : Inheritance In Java – Part 1  

4th Sep, 2009 | View Comments | Posted in Java by Alex Jose
Share
 

 

Java supports inheritance that enables a class to inherit data members and methods from another class. Inheritance enables you to reuse the functionalities and capabilities of the existing class by extending a new class from the existing class and adding new features to it.

Introduction to Inheritance

In inheritance, the class that inherits the data members and methods from another class is known as the subclass. The class from which the subclass inherits is known as the superclass. The superclass is also referred to as the base class, and the subclass is referred to as the derived class. You can create additional data members and methods to add more features in a subclass. A superclass can also be a subclass of another class.

The superclass represents the generalized properties, which are required for the hierarchy of the superclass. The subclasses represent specialization, in which data members and methods specific for the subclass are added to the inherited data members and the superclass methods.

Implementing Different Types of Inheritance

Java supports following types of inheritance:

  • Single Level Inheritance
  • Multilevel Inheritance

Single Level Inheritance

In single level inheritance, a subclass is derived from a single superclass. For example, subclasses B and C inherit the properties of a single superclass, A. The following figure shows the structure of single level inheritance:

single level inheritance

Single Level Inheritance

For example, the AirTicket class has data members and methods. Examples of data members are flight number, date, time, destination. The flightInfo() method of the AirTicket class displays information about a flight. An air ticket is of two types, confirmed ticket and requested ticket.

The ConfirmedTicket subclass that extends from the AirTicket class, inherits its data members. The ConfirmedTicket subclass contains seat number as an additional data member and the displaySeatInfo() method that displays the seat number of the confirmed ticket.

The RequestTicket subclass that extends from the AirTicket class also inherits its data members. The RequestTicket subclass contains status as an additional data member and the displayStatus() method that displays the status of the request ticket.

The following figure shows the concept of inheritance in the AirTicket class:

Concept of Inheritance

Concept of Inheritance

Related Posts with Thumbnails
  • http://www.rockingteam.com/2009/09/java-tutorial-inheritance-in-java-part-2-example/ Java Tutorial : Inheritance in Java – Part 2, Example – Rocking Team

    [...] In the last article we saw the inheritance in java. [...]

  • http://www.rockingteam.com/2009/09/java-tutorial-inheritance-and-interfaces-summary/ Java Tutorial : Inheritance and Interfaces – Summary – Rocking Team

    [...] Java Tutorial : Inheritance In Java – Part 1 [...]

blog comments powered by Disqus