You are on page 1of 11

Chapter 3.

Extending Classes and Inheritance


Inheritance
Inheritance is one of the features of Object Oriented Programming System(OOPs).

Inheritance in Java is a mechanism in which one object acquires all the properties and
behaviors of a parent object. It is an important part of OOPs (Object Oriented programming
system).

The idea behind inheritance in Java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
the parent class. Moreover, you can add new methods and fields in your current class also.

Terms used in Inheritance

 Class: A class is a group of objects which have common properties. It is a template or


blueprint from which objects are created.
 Sub Class/Child Class: Subclass is a class which inherits the other class. It is also
called a derived class, extended class, or child class.
 Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
 Reusability: As the name specifies, reusability is a mechanism which facilitates you
to reuse the fields and methods of the existing class when you create a new class. You
can use the same fields and methods already defined in the previous class.

3.1 Use and Benefits of Inheritance in OOP

The syntax of Java Inheritance

1. class Subclass-name extends Superclass-name


2. {
3. //methods and fields
4. }

The extends keyword indicates that you are making a new class that derives from an existing
class. The meaning of "extends" is to increase the functionality.

In the terminology of Java, a class which is inherited is called a parent or superclass, and the
new class is called child or subclass.
Java Inheritance Example

As displayed in the above figure, Programmer is the subclass and Employee is the superclass.
The relationship between the two classes is Programmer IS-A Employee. It means that
Programmer is a type of Employee.

class Employee{

float salary=40000;

class Programmer extends Employee{

int bonus=10000;

public static void main(String args[]){

Programmer p=new Programmer();

System.out.println("Programmer salary is:"+p.salary);

System.out.println("Bonus of Programmer is:"+p.bonus);

Output

Programmer salary is:40000.0


Bonus of programmer is:10000
In the above program , Programmer object can access the field of own class as well as of
Employee class i.e. code reusability.

Benefits of Inheritance in OOP

1. Reusability : Inheritance help the code to be reused in many situations. The base class is
defined and once it is compiled , it need not be reworked. Using the concept of
inheritance , the programmer can create as many derived classes from the base class as
needed while adding specific features to each derived class as needed.
2. Save time and effort : The above concept of reusability achieved by inheritance saves
the programmer time and effort. Since the main code written can be reused in various
situations as needed.
3. Data Hiding : The base class can decide to keep some data private so that it cannot be
altered by the derived class.
4. Extensibility : The base class logic can be extended as per the business logic of the
derived class.
5. Easy to understand: It is easy to understand large and complex programs with use of
inheritance.
6. Reliability : Increases program structure which results in greater reliability.
7. Maintainability : It is easy to debug a program when divided into parts
3.2 Types of Inheritance in Java
There can be three types of inheritance in java: single, multilevel and hierarchical. In java
programming, multiple and hybrid inheritance is supported through interface only.

Single Inheritance
In Single Inheritance one class extends another class (one class only).

In above diagram, Class B extends only Class A. Class A is a super class and Class B is a
Sub-class.
Program
class A{
int a = 10;
void show() {
System.out.println("a = "+a);
}
}

public class B extends A{

public static void main(String[] args) {


B b = new B();
b.show();

}
}

Output
a=10
Here, we can see that show() method is declared in class A, but using child class object, we
can call it. That shows the inheritance between these two classes.

Multilevel Inheritance:

In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived class
becomes the base class for the new class.

As per shown in diagram a class C extends to class B that also extends to class A and all the
data members an methods of class A and B are now accessible in class C.

Program
class A{
int a = 10;
void show() {
System.out.println("a = "+a);
}
}

class B extends A{
int b = 10;
void showB() {
System.out.println("b = "+b);
}
}

public class C extends B{

public static void main(String[] args) {


C c = new C();
c.show();
c.showB();
}
}

Output

a=10

b=10

Hierarchical Inheritance:

In Hierarchical Inheritance, one class is inherited by many sub classes.

Hierarchical Inheritance

As per above example, Class B, C, and D inherit the same class A. In this case class B ,C and
D share properties of class A.
Program

class A{
int a = 10;
void show() {
System.out.println("a = "+a);
}
}

class B extends A{
int b = 10;
void showB() {
System.out.println("b = "+b);
}
}

public class C extends A{

public static void main(String[] args) {


C c = new C();
c.show();
B b = new B();
b.show();
}
}

Output

a = 10

a = 10

3.3 Inheriting Data members and Methods

Super keyword

In Java, super keyword is used to refer to immediate parent class of a child class. In other
words super keyword is used by a subclass whenever it need to refer to its immediate super
class.

Example of Child class referring Parent class property using super keyword

In this examle we will focus on accessing the parent class data member.
class Parent
{
String name;

}
public class Child extends Parent {
String name;
public void details()
{
super.name = "Parent"; //refers to parent class member
name = "Child";
System.out.println(super.name+" and "+name);
}
public static void main(String[] args)
{
Child cobj = new Child();
cobj.details();
}
}

Output

Parent and Child

Example of Child class refering Parent class methods using super keyword

In this examle we will only focus on accessing the parent class methods.
class Parent {
String name;
public void details()
{
name = "Parent";
System.out.println(name);
}
}
public class Child extends Parent {
String name;
public void details()
{
super.details(); //calling Parent class details() method
name = "Child";
System.out.println(name);
}
public static void main(String[] args)
{
Child cobj = new Child();
cobj.details();
}
}

Output
Parent
Child

3.4 Role of Constructors in inheritance

In Java, constructor of base class with no argument gets automatically called in derived class
constructor. For example, output of following program is:

Base Class Constructor Called


Derived Class Constructor Called
class Base {

Base() {

System.out.println("Base Class Constructor Called ");

class Derived extends Base {

Derived() {

System.out.println("Derived Class Constructor Called ");

public class Main {

public static void main(String[] args) {

Derived d = new Derived();

But, if we want to call parameterized contructor of base class, then we can call it using
super(). The point to note is base class constructor call must be the first line in derived
class constructor. For example, in the following program, super(_x) is first line derived class
constructor.

// filename: Main.java
class Base {

int x;

Base(int _x) {

x = _x;

class Derived extends Base {

int y;

Derived(int _x, int _y) {

super(_x);

y = _y;

void Display() {

System.out.println("x = "+x+", y = "+y);

public class Main {

public static void main(String[] args) {

Derived d = new Derived(10, 20);

d.Display();

Output:
x = 10, y = 20
Note: When calling the parent class constructor from the child class using super keyword,
super keyword should always be the first line in the method/constructor of the child class.

You might also like