Categories: Java

Inheritance in Java

In Java, inheritance is a mechanism by which one object inherits all of its parent’s properties and behaviors. It is an essential component of OOPs (Object-Oriented programming systems).

You can use the parent class’s methods and fields when you inherit from an existing class. You can also add new methods and fields to your current class.

The IS-A relationship, also known as a parent-child relationship, is represented by inheritance.

Uses of inheritance in java:
  • Overriding Methods (so runtime polymorphism can be achieved).
  • For reusability of code.
Key Terms:
  • class: A class is a collection of objects with similar properties. It is a blueprint or template from which objects are made.
  • Subclasses/Child Classes: A subclass is a class that inherits from another. A derived class, extended class, or child class is another name.
  • Superclass/ parent-class: The superclass or parent class is the class from which a subclass inherits its features. It’s also known as a parent class or a base class.
  • Reusability: As the name implies, reusability is a mechanism that allows you to reuse existing class fields and methods when creating a new class. The fields and methods defined in the previous class can be reused.
Syntax:
class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

The extends keyword indicates that you’re creating a new class from an existing one.

Types of inheritance in Java:

In Java, there are three types of inheritance based on class: single, multilevel, and hierarchical inheritance.

1)Simple inheritance:

A single inheritance occurs when one class inherits from another.

Example:

class Institute{  
void member(){System.out.println("coderz");}  
}  
class Student extends Institute{  
void study(){System.out.println("Studying ");}  
}  
class TestInheritance{  
public static void main(String args[]){  
Student d=new Student();  
d.member();  
d.study();  
}
}  

Output:

coderz
Studying 
Multi-level inheritance:

Multilevel inheritance occurs when there is a chain of inheritance.

Example:

class plants{  
void grow(){System.out.println("plant KIngdom");}  
}  
class Fruit extends plants{  
void healthy(){System.out.println("Fruits are healthy");}  
}  
class Apple extends Fruit{  
void is_fruit(){System.out.println("Apple is a fruit");}  
}  
class TestInheritance2{  
public static void main(String args[]){  
Apple d=new Apple();  
d.grow();  
d.healthy();  
d.is_fruit();  
}}  

Output:

plant KIngdom
Fruits are healthy
Apple is a fruit
Hierarchical Inheritance:

Hierarchical inheritance occurs when two or more classes inherit a single class.

Example:

class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void bark(){System.out.println("barking...");}  
}  
class Cat extends Animal{  
void meow(){System.out.println("meowing...");}  
}  
class TestInheritance3{  
public static void main(String args[]){  
Cat c=new Cat();  
c.meow();  
c.eat();  
//c.bark();//C.T.Error  
}} 

Output:

meowing...
eating...

Why multiple inheritance is not supported in java?

Multiple inheritances are not supported in Java to reduce complexity and simplify the language.

Super Keyword:


In Java, the super keyword is a reference variable that refers to an immediate parent class object.

When you create a subclass instance, you also create an instance of the parent class, which is referred to by the super reference variable.

Application of Java super:

  • To refer to an immediate parent class instance variable, use the keyword super.
  • super can be used to call the method of an immediate parent class.
  • super() can be used to call the constructor of the immediate parent class.
  • Example:
class Animal{  
void eat(){System.out.println("eating...");}  
}  
class Dog extends Animal{  
void eat(){System.out.println("eating bread...");}  
void bark(){System.out.println("barking...");}  
void work(){  
super.eat();  
bark();  
}  
}  
class TestSuper2{  
public static void main(String args[]){  
Dog d=new Dog();  
d.work();  
}}  

Output:

eating...
barking...

Note: also read about the Non-access modifiers

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago