INHERITANCE IN JAVA
- -You might have heard people saying your nose is similar to your father or mother. Or, more formally, we can say that you've inherited the genes from your parents due to which you look similar to them.
- -The same phenomenon of inheritance is also valid in programming.
- -In Java, one class can easily inherit the attributes and methods from some other class. This mechanism of acquiring objects and properties from some other class is known as inheritance in Java.
- -Inheritance is used to borrow properties & methods from an existing class.
- -Inheritance helps us create classes based on existing classes, which increases the code's reusability.
Terms used in Inheritance
- Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
- Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
- Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
- Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
The syntax of Java Inheritance
Types of inheritance in java
When one class inherits multiple classes, it is known as multiple inheritance. For Example:
Single Inheritance Example
When a class inherits another class, it is known as a single inheritance. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.
EXAMPLE:-
package own_pratice;
class Animal{
void eat()
{
System.out.println("eating...");}
}
class Dog extends Animal{
void bark(){
System.out.println("barking...");}
}
public class Inheritanc2 {
public static void main(String args[]){
ballpen bp = new ballpen();
bp.eat();
bp.bark();
}
}
OUTPUT:-
Multilevel Inheritance Example
When there is a chain of inheritance, it is known as multilevel inheritance. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.
EXAMPLE:-
package own_pratice;
class Animal{
void eat()
{
System.out.println("eating...");}
}
class Dog extends Animal{
void bark()
{
System.out.println("barking...");}
}
class BabyDog extends Dog{
void weep()
{
System.out.println("weeping...");}
}
public class Inheritanc2 {
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
OUTPUT:-
Hierarchical Inheritance Example
When two or more classes inherits a single class, it is known as hierarchical inheritance. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.
INPUT:-
package own_pratice;
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...");}
}
public class Inheritanc2 {
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}
}
OUTPUT:-
0 Comments