top of page

Inheritance in java(OPP concept)

nishaghadiya22

Java, Inheritance is an important concept 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.


Types of Inheritance :



Java supports hybrid Inheritance. However, Java does not support Multiple Inheritance with classes, so in order to achieve hybrid inheritance, multiple inheritances with classes should not be used. Multiple Inheritance with Interfaces is possible.


14 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Comments


bottom of page