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.
class Subclass-name extends Superclass-name
{
//methods and fields
}
The extends keyword indicates that you’re creating a new class from an existing one.
In Java, there are three types of inheritance based on class: single, multilevel, and hierarchical 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
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 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.
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:
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
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…