Inheritance in Java. In English

                      Inheritance


What is java inheritance?................

Inheritance is the transfer of features and behaviors from the superclass to the subclass. It is possible for Java classes to inherit methods and attributes from one another. Two categories make up the "inheritance concept".

  • Subclass (child) - the class that has other classes as inheritors.
  • Superclass (Parent) - the class that is being inherited from.

All subclasses share the code defined in the Superclass. The parent class code can be used directly by child classes.

Only inheritance allows for Method Overriding. It is one approach for Java to implement run-time polymorphism.

Inheritance allows us to reach the concept of abstraction, where we are not need to disclose all the details. Abstraction only makes the user aware of the capability.

Use the extends keyword to inherit from a class.

Syntax: 

           class derived-class extends base-class

          { 

          //methods and fields  

          }

Example: -

              


Inheritance types .......................

There are five different types of inheritance.

         1.Single Inheritance
          2.Multilevel Inheritance
          3.Hierarchical Inheritance
          4.Multiple Inheritance
          5.Hybrid Inheritance

1.Single inheritance
    In single inheritance, features from one superclass are inherited by subclasses.

Example: -

                          class A {
                             public void print_java()
                          {
                              System.out.println("JAVA");
                          }
                        }
 
                       class B extends A{
                           public void print_for() 
                          { 
                              System.out.println("for"); 
                          }
                        }
                       public class Main {
                             public static void main(String[] args)
                            {
                                B b = new B();
                                b.print_java();
                                b.print_for();
                                b.print_java();
                             }
                         }


                       Output - JAVA

                                for

                               JAVA

2.Multilevel inheritance
In Multilevel Inheritance, a super class will be inherited by a derived class, which also serves as the super class for other classes.

Example: -

                        class A {
                             public void print_school()
                          {
                              System.out.println("SCHOOL");
                          }
                        }
 
                       class B extends A {
                           public void print_for() 
                          { 
                              System.out.println("for"); 
                          }
                        }
                       public class Main {
                             public static void main(String [] args)
                            {
                                B b = new B();
                                b.print_school();
                                b.print_for();
                                b.print_school();
                             }
                         }


                       Output - SCHOOL

                                for

                               SCHOOL

3.Hierarchical inheritance

In hierarchical inheritance, one class functions as the superclass for multiple subclasses.

Example: -

class One { public void print_one() {
System.out.println(" 1");
} } class Two extends One { public void print_Two() {
System.out.println(" 2");
} } class Three extends One { public void print_Three() {
System.out.println(" 3");
} } class Four extends One { public void print_Four() {
System.out.println(" 4");
} } public class Test { public static void main(String [] args) { Two obj_B = new Two(); obj_B.print_One(); obj_B.print_Two(); Three obj_C = new Three(); obj_C.print_One(); obj_C.print_Three(); Four obj_D = new Four(); obj_D.print_One(); obj_D.print_Four(); } }

Output - 1
2
1
3
1
4
4.Multiple Inheritance 
In multiple inheritance, one class can inherit characteristics from all of its parent classes and have more than one superclass. Please be aware that multiple inheritances using classes are not supported by Java. Only by using interfaces can we obtain multiple inheritances in Java.

Example: -
            
interface one {
     public void print_java();
}
 
interface two {
     public void print_for();
}
 
interface three extends one, two {
     public void print_java();
}
class child implements three {
     public void print_java()
     {
         System.out.println("JAVA");
     }
 
     public void print_for() { System.out.println("for");
}
}
public class Main {
     public static void main(String [] args)
     {
         child c = new child();
         c.print_java();
         c.print_for();
         c.print_java();
     }
}
Output - JAVA
for
JAVA

5.Hybrid inheritance

It is a combination of two or more of the aforementioned inheritance categories.
Hybrid inheritance that involves multiple inheritance is likewise not possible with
classes because Java does not support it.If we wish to implement hybrid inheritance in Java and use multiple inheritance,
we can only do it using interfaces.
It's vital to remember that hybrid inheritance does not necessarily call for the
exclusive usage of multiple inheritance.
A combination of Multilevel Inheritance and Hierarchical Inheritance with Classes,
Hierarchical and Single Inheritance with Classes, and both are possible.
As a result, hybrid inheritance can be implemented without the use of several
inheritance types by simply using classes.

Example: -
class SolarSystem { } class Earth extends SolarSystem { } class Mars extends SolarSystem { }
class Moon extends Earth { public static void main(String args[]) { SolarSystem s = new SolarSystem(); Earth e = new Earth(); Mars m = new Mars(); System.out.println(s instanceof SolarSystem); System.out.println(e instanceof Earth); System.out.println(m instanceof SolarSystem); } }

Output - true
true
true

Advantages of inheritence ......................

1.Code reuse is made possible by inheritance, which lowers the amount of new
code required. Reduced code duplication results from the subclass's ability
to reuse the superclass's properties and functions.

2.The use of inheritance enables the development of abstract classes that specify
a common interface for a collection of related classes. As a result, the code is
easier to maintain and extend and encourages abstraction and encapsulation.

3.Polymorphism, or an object's capacity to assume various forms, is made
possible by inheritance. Because subclasses have the ability to override the
superclass's methods, they can alter how the superclass behaves.

4.A class hierarchy can be created through inheritance and used to represent
real-world objects and their connections.

Disadvantages of inheritance .......................

1.The complexity and difficulty of the code can increase due to inheritance. This
is particularly true if there are many inheritances used or if the inheritance
hierarchy is complex.

2.It is challenging to make changes to the superclass without affecting the
subclass due to inheritance's close connection between the two classes.

Comments

Popular posts from this blog

Lambda expression in Java. In English

Loops in Java. In English

Conditions in java. In English