0% found this document useful (0 votes)
2 views

Inheritance_in_Java

Inheritance in Java is a key concept of Object-Oriented Programming that allows one class to inherit features from another, promoting code reusability and abstraction. It can take various forms, including single, multilevel, hierarchical, multiple (through interfaces), and hybrid inheritance. While inheritance offers advantages like code reuse and polymorphism, it can also introduce complexity and tight coupling between classes.

Uploaded by

35421102023
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)
2 views

Inheritance_in_Java

Inheritance in Java is a key concept of Object-Oriented Programming that allows one class to inherit features from another, promoting code reusability and abstraction. It can take various forms, including single, multilevel, hierarchical, multiple (through interfaces), and hybrid inheritance. While inheritance offers advantages like code reuse and polymorphism, it can also introduce complexity and tight coupling between classes.

Uploaded by

35421102023
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/ 3

Inheritance in Java

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 super class 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.

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 super class(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 super class 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?


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.

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.
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.

3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a super class (base class) for more than one
subclass.

4. Multiple Inheritance (Through Interfaces)


In Multiple inheritances, one class can have more than one superclass and inherit features from
all parent classes.

5. Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. hybrid inheritance involving
multiple inheritance is also not possible with classes. In Java, we can achieve hybrid
inheritance only through Interfaces if we want to involve multiple inheritance to implement
Hybrid inheritance.

IS-A type of Relationship


IS-A is a way of saying: This object is a type of that object.

Advantages Of Inheritance in Java:


1. Code Reusability: Inheritance allows for code reuse and reduces the amount of code that
needs to be written. The subclass can reuse the properties and methods of the superclass,
reducing duplication of code.
2. Abstraction: Inheritance allows for the creation of abstract classes that define a common
interface for a group of related classes. This promotes abstraction and encapsulation,
making the code easier to maintain and extend.
3. Class Hierarchy: Inheritance allows for the creation of a class hierarchy, which can be used
to model real-world objects and their relationships.
4. Polymorphism: Inheritance allows for polymorphism, which is the ability of an object to
take on multiple forms. Subclasses can override the methods of the superclass, which
allows them to change their behavior in different ways.

Disadvantages of Inheritance in Java:


1. Complexity: Inheritance can make the code more complex and harder to understand. This is
especially true if the inheritance hierarchy is deep or if multiple inheritances is used.
2. Tight Coupling: Inheritance creates a tight coupling between the super class and subclass,
making it difficult to make changes to the superclass without affecting the subclass.

You might also like