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

Inheritence in Java

Inheritance is a mechanism in object-oriented programming where a subclass inherits properties and features from a superclass using the 'extends' keyword. It can be categorized into single, multi-level, hierarchical, multiple, and hybrid inheritance, with Java supporting multiple and hybrid inheritance through interfaces. Examples illustrate single, multi-level, and hierarchical inheritance, demonstrating parent-child relationships in class structures.

Uploaded by

thinkpositive670
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)
10 views7 pages

Inheritence in Java

Inheritance is a mechanism in object-oriented programming where a subclass inherits properties and features from a superclass using the 'extends' keyword. It can be categorized into single, multi-level, hierarchical, multiple, and hybrid inheritance, with Java supporting multiple and hybrid inheritance through interfaces. Examples illustrate single, multi-level, and hierarchical inheritance, demonstrating parent-child relationships in class structures.

Uploaded by

thinkpositive670
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

Inheritance

Inheritance is the mechanism by which one class acquires the properties and features of another class.
The class that inherits the properties is called a sub-class (child class) while the class from which the
property is inherited is called the super-class (parent class).

A child class inherits properties of the parent class with the help of the extends keyword.

Syntax:
class childClass extends parentClass {

// any code

Inheritance can be further divided into the following types:


 Single level

 Multi-level

 Hierarchical

 Multiple

 Hybrid

Multiple and hybrid inheritance is not directly supported in Java, instead, it is achieved through the use
of interfaces in Java.

i. Single Inheritance

When a single class inherits the attributes and methods of another class, it is known as single
inheritance.
Example:

class FundamentalForce {

void Force() {

[Link]("There are four fundamental forces.");

class Gravitational extends FundamentalForce {

void Gravity() {

[Link]("Fruits fall to the ground due to gravitational Force.");

class SingleInheritance {

public static void main(String[] args) {

Gravitational G = new Gravitational();

[Link]();

[Link]();
} }

Output:
There are four fundamental forces.

Fruits fall to the ground due to gravitational Force.

In this example, we see how class Gravitational can inherit the method of its parent class
(FundamentalForce).

This is a perfect example of a parent-child relationship.

ii. Multi-level Inheritance


When a class 3 inherits attributes and methods from class 2 which in turn inherits its attributes and
methods from class 1, it is called multi-level inheritance.

It forms a child-parent-grandparent (or a parent-child-grandchild) relationship. Meaning that the child


inherits from the parent while the parent inherits from the grandparent.
Example:

class NuclearForce extends FundamentalForce {

void Nuclear() {

[Link]("Nuclear Forces are of two types;");

[Link]("Strong Nuclear Force");

[Link]("Weak Nuclear Force");

class StrongNuclearForce extends NuclearForce {

void Strong() {

[Link]("Strong Nuclear Force is responsible for the underlying stability of matter.");


}

class MultilevelInheritance {

public static void main(String[] args) {

StrongNuclearForce st = new StrongNuclearForce();

[Link]();

[Link]();

[Link]();

Output:
There are four fundamental forces.

Nuclear Forces are of two types;

Strong Nuclear Force

Weak Nuclear Force

Strong Nuclear Force is responsible for the underlying stability of matter.

In this example, we see how class StrongNuclearForce inherits the method of NuclearForce which in
turn inherits the method of FundamentalForce. This is a classic example of a child-parent-grandparent
relationship.

iii. Hierarchical Inheritance


Hierarchical inheritance is when two or more classes inherit from a single class. This can be easily
visualized as a parent with more than one child. Here each child can inherit the properties of a parent.
Example:

class FundamentalForce {

void Force() {

[Link]("There are four fundamental forces.");

class Gravitational extends FundamentalForce {

void Gravity() {

[Link]("Fruits fall to the ground due to gravitational Force.");

class Electromagnetic extends FundamentalForce {

void Particles() {

[Link]("The electromagnetic force acts between charged particles");

class HierarchicalInheritance {

public static void main(String[] args) {


[Link]("Child 1:");

Gravitational G = new Gravitational();

[Link]();

[Link]();

[Link]();

[Link]("Child 2");

Electromagnetic em = new Electromagnetic();

[Link]();

[Link]();

Output:
Child 1:

There are four fundamental forces.

Fruits fall to the ground due to gravitational Force.

Child 2

There are four fundamental forces.

The electromagnetic force acts between charged particles

As we can see, both the children can access the method of the parent class.

You might also like