Home > Java > Inheritance > Introduction
Introduction

Introduction to Inheritance

Inheritance is one of the most crucial concepts in object-oriented programming, and it has a direct effect on how you design and write your own Java classes.


Inheritance is a mechanism that enables one class to inherit all the behavior and attributes of another class.


Through inheritance, a class immediately picks up all the functionality of an existing class. Because of this, you only must define how the new class is different from an existing class.


With inheritance, all classes—those you create and those from the Java class library and other libraries—are arranged in a strict hierarchy.


A class that inherits from another class is called a subclass. The class that gives the inheritance is called a superclass.


A class can have only one superclass, but each class can have an unlimited number of subclasses. Subclasses inherit all the attributes and behavior of their superclasses.


In practical terms, this means that if the superclass has behavior and attributes that your class needs, you don’t have to redefine it or copy that code to have the same behavior and attributes. Your class automatically receives these things from its superclass, the superclass gets them from its superclass, and so on, all the way up the hierarchy. Your class becomes a combination of its own features and all the features of the classes above it in the hierarchy.


At the top of the Java class hierarchy is the class Object—all classes inherit from this superclass. Object is the most general class in the hierarchy, and it defines behavior inherited by all the classes in the Java class library. It turns out that you’re always doing inheritance when you create a class, because unless you explicitly inherit from some other class, you implicitly inherit from Java’s standard root class Object.


Benefits of inheritance are as follows:

 

  • Inheritance increases your ability to reuse classes. Software can be extended by reusing previously defined classes and adding new methods to the subclasses.
  • Inheritance increases the level of abstraction in a program.
  • Inheritance improves the clarity in the design of classes by allowing the implementation of methods to be postponed in superclasses and to appear in subclasses


The superclass object can accept reference of subclass. This is called upcasting.

 

Only public and protected data members and methods of superclass are inherited in subclass. Private data members and methods of superclass are not inherited in subclass.


Syntax


class SuperClass {
    //fields, constructors and method declarations
}


class SubClass extends SuperClass {
    //fields, constructors and methods declarations
}


You state this in code by giving the name of the class as usual, but before the opening brace of the class body, put the keyword extends followed by the name of the base class. When you do this, you automatically get all the data members and methods in the base class.


Example


1.  /**
2.  * @(#)InheritanceDemo.java
3.  *
4.  *
5.  * @author
6.  * @version 1.00 2009/11/18
7.  */
8.
9.  class Employee {
10.
11.    // constant
12.    protected final static float holidayEntitlement = 20.0f;
13.    // instance variables
14.    protected String employeeName;
15.    protected String employeeDept;
16.    protected int lengthOfService;
17.
18.    // constructor
19.    public Employee(String name, String department, int yearsService) {
20.        employeeName = name;
21.        employeeDept = department;
22.        lengthOfService = yearsService;
23.    }
24.
25.    // instance methods
26.    public String getName() {
27.        return employeeName;
28.    }
29.    
30.    public String getDepartment() {
31.        return employeeDept;
32.    }
33.    
34.    public int getLengthOfService()    {
35.        return lengthOfService;
36.    }
37.    
38.     public float getHolidays()    {
39.        return holidayEntitlement;
40.    }
41.    
42.  }
43.
44.  class Technician extends Employee {
45.    
46.    // instance variable
47.    protected float holidays;
48.
49.    // constructor
50.    public Technician(String name, String department, int yearsService) {
51.        
52.        // Call parent class Employee constructor
53.        super(name, department, yearsService);
54.        
55.    }
56.  }
57.
58.  public class InheritanceDemo {
59.        
60.    /**
61.     * Creates a new instance of InheritanceDemo.
62.     */
63.    public InheritanceDemo() {
64.    }
65.    
66.    /**
67.     * @param args the command line arguments
68.     */
69.    public static void main(String[] args) {
70.       // TODO code application logic here
71.        
72.       
73.        // instantiate an employee and a technician
74.        Employee caterer = new Employee("Millie Johnson","Catering", 7);
75.        
76.        Technician lineWorker = new    Technician("Susan Schroeder","Electronics", 4);
77.        
78.        // use methods of superclass to display details of employee
79.        System.out.println("Using the superclass methods");
80.        System.out.println("Name: "+caterer.getName());
81.        System.out.println("Department: "+caterer.getDepartment());
82.        System.out.println("Service: "+caterer.getLengthOfService()+" years");
83.        System.out.println("Holidays: "+caterer.getHolidays()+" days");
84.        
85.        // use inherited methods of superclass to display details of a
86.        // technician
87.        System.out.println("nUsing the inherited superclass methods");
88.        System.out.println("Name: "+lineWorker.getName());
89.        System.out.println("Department: "+ lineWorker.getDepartment());
90.        System.out.println("Service: "+lineWorker.getLengthOfService()+" years");
91.        System.out.println("Holidays: "+lineWorker.getHolidays()+    " days");
92.        
93.    }
94. }


Output


Using the superclass methods
Name: Millie Johnson
Department: Catering
Service: 7 years
Holidays: 20.0 days



Using the inherited superclass methods
Name: Susan Schroeder
Department: Electronics
Service: 4 years
Holidays: 20.0 days

Leave a Reply

Fields with * are mandatory.
* Name:
Website:
* Comments:
 
Tags allowed: <strong> <i> <u>
* Code:
 
 
Submit   Cancel