0% found this document useful (0 votes)
1 views7 pages

Inheritance

Inheritance in Java is a core concept of Object-Oriented Programming that allows one class to inherit features from another, promoting code reusability and enabling method overriding and abstraction. Key terminologies include superclass (parent class) and subclass (child class), with various types of inheritance such as single, multilevel, and hierarchical. The 'super' keyword is used to refer to the parent class and invoke its methods and constructors within the subclass.

Uploaded by

ipsitmahakul22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Inheritance

Inheritance in Java is a core concept of Object-Oriented Programming that allows one class to inherit features from another, promoting code reusability and enabling method overriding and abstraction. Key terminologies include superclass (parent class) and subclass (child class), with various types of inheritance such as single, multilevel, and hierarchical. The 'super' keyword is used to refer to the parent class and invoke its methods and constructors within the subclass.

Uploaded by

ipsitmahakul22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Inheritance in JAVA Part1

In Java, Inheritance is an important pillar of OOP(Object-Oriented Programming). It is the


mechanism in Java by which one class is allowed to inherit the features(fields and methods)
of another class. In Java, Inheritance means creating new classes based on existing ones. A
class that inherits from another class can reuse the methods and fields of that class. In
addition, you can add new fields and methods to your current class as well.

Why Do We Need Java Inheritance?


 Code Reusability: The code written in the Superclass is common to all subclasses. Child
classes can directly use the parent class code.
 Method Overriding: Method Overriding is achievable only through Inheritance. It is one of
the ways by which Java achieves Run Time Polymorphism.
 Abstraction: The concept of abstract where we do not have to provide all details, is
achieved through inheritance. Abstraction only shows the functionality to the user.

Important Terminologies Used in Java Inheritance


 Class: Class is a set of objects which shares common characteristics/ behavior and
common properties/ attributes. Class is not a real-world entity. It is just a template or
blueprint or prototype from which objects are created.
 Super Class/Parent Class: The class whose features are inherited is known as a
superclass(or a base class or a parent class).
 Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields and
methods in addition to the superclass fields and methods.
 Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create
a new class and there is already a class that includes some of the code that we want, we
can derive our new class from the existing class. By doing this, we are reusing the fields
and methods of the existing class.

How to Use Inheritance in Java?


The extends keyword is used for inheritance in Java. Using the extends keyword indicates
you are derived from an existing class. In other words, “extends” refers to increased
functionality.
Syntax :
class DerivedClass extends BaseClass
{
//methods and fields
}

Java Inheritance Types


Below are the different types of inheritance which are supported by Java.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
1. Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the
properties and behavior of a single-parent class. Sometimes, it is also known as simple
inheritance. In the below figure, ‘A’ is a parent class and ‘B’ is a child class. The class ‘B’
inherits all the properties of the class ‘A’.
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the
derived class also acts as the base class for other classes. In the below image, class A serves
as a base class for the derived class B, which in turn serves as a base class for the derived
class C. In Java, a class cannot directly access the grandparent’s members.
3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one
subclass. In the below image, class A serves as a base class for the derived classes B, C,
and D.

Constructor execution in inheritance


A constructor is a block of code used to initialize an object. It is called when an object of a class is
created. Constructors have the same name as the class and no return type.

In inheritance, a subclass inherits the properties and methods of its superclass. However,
constructors are not inherited by the subclass. The subclass must call the constructor of
the superclass explicitly or implicitly. The superclass constructor is always invoked when
a subclass object is created.

When a subclass is instantiated, the following happens:

1. The subclass constructor is invoked.


2. The superclass constructor is called either explicitly using super() or implicitly.

3. The subclass constructor completes its execution.

By default, the subclass constructor calls the superclass constructor implicitly. Java automatically
inserts a call to the no-argument constructor of the superclass.

Super keyword
The super keyword in Java is a reference variable which is used to refer immediate parent class object.

Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by
super reference variable.

Usage of Java super Keyword

1. super can be used to refer immediate parent class instance variable.


2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

You might also like