What is Inheritance in Java

An overview of Inheritance In Java:

Inheritance in Java means one class legally acquires properties from another class.

Inheritance allows the reuse of a particular class, also known as a superclass, to create a new class or subclass. The new class can acquire the properties of the superclass.

To understand the concept of inheritance, we can correlate it with real-life examples. For instance, the animal class contains a subclass of vertebrates and invertebrates. They are further classified into mammals, reptiles, fish, etc. However, all these animals contain certain attributes that are common to the animal kingdom.

UML Notation for Inheritance in java:

UML Notation of Inheritance

  • A extends B
  • While the class ‘A’ shares the properties,
  • Class ‘B’ inherits or acquires the properties.
  • The keyword “extends” is the term between these two classes.

What is a Super Class in Java?

The class that shares its properties (in this case, A class) is known as a base class, parent class, or superclass.

What is a Sub Class in Java?

The class that acquires the properties from another class is the child, acquired, or derived class.

  • This is how the concept of inheritance is depicted in Java.
  • Moreover, B acquires the properties of A.
  • However, class A cannot acquire or access the properties of B.

How to write a program for inheritance in Java?

class A{}

class B extends A

{

          public static void main (string [] args);

        {

        }

}

Object Creation from Simple Inheritance in Java:

In simple inheritance, there are four possibilities for creating an object.

For example,

1) Super Class Objects:

A a = new A(); Superclass Object

Discover more from BerylSoft

Subscribe now to keep reading and get access to the full archive.

Continue reading