What is Inheritance?

What is Inheritance?

Asked on January 18, 2019 in Java.
Add Comment


  • 4 Answer(s)

    Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class.

    Existing class is known as Super class whereas the derived class is known as a sub class.

    Example:

    1 Super class:
    2

    public class Manupulation()

    {

    3 }
    4 Sub class:
    5 public class Addition extends Manipulation(){
    6 }

    Inheritance is applicable for public and protected members only. Private members can’t be inherited.

    Answered on January 18, 2019.
    Add Comment

    A class inherits fields and methods from all its superclasses, whether direct or indirect. A subclass can override methods that it inherits, or it can hide fields or methods that it inherits. (Note that hiding fields is generally bad programming practice.)
    class Parent{
    // Private method
    private void method1(){
    System.out.println(“In private method of Parent class”);
    }
    void method2(){
    // calling private method
    method1();
    }
    void method3(){
    // calling private method
    method1();
    }
    }

    class Child extends Parent{

    }
    class MainClass{
    public static void main(String[] args){
    Child child = new Child();
    // calling non-private method which internally calls the private method
    child.method2();
    }
    }

    Answered on January 25, 2019.
    Add Comment

    inheritance

    Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes.

    A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses.

    Types of Inheritance

    There are Various types of inheritance in Java:

      1. Single Inheritance:

    In Single Inheritance one class extends another class (one class only).

    Types of Inheritance

    In above diagram, Class B extends only Class A. Class A is a super class and Class B is a Sub-class.

      1. Multiple Inheritance:

    In Multiple Inheritance, one class extending more than one class. Java does not support multiple inheritance.

    Types of Inheritance

    As per above diagram, Class C extends Class A and Class B both.

      1. Multilevel Inheritance:

    In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived class becomes the base class for the new class.

    Types of Inheritance

    As per shown in diagram Class C is subclass of B and B is a of subclass Class A.

      1. Hierarchical Inheritance:

    In Hierarchical Inheritance, one class is inherited by many sub classes.

    Types of Inheritance

    As per above example, Class B, C, and D inherit the same class A.

      1. Hybrid Inheritance:

    Hybrid inheritance is a combination of Single and Multiple inheritance.

    Types of Inheritance

    As per above example, all the public and protected members of Class A are inherited into Class D, first via Class B and secondly via Class C.

     

    Java Inheritance Example

    Concept of Inheritance Java and Java Polymorphism

    class Doctor {
     void Doctor_Details() {
      System.out.println("Doctor Details...");
     }
    }
    
    class Surgeon extends Doctor {
     void Surgeon_Details() {
      System.out.println("Surgen Detail...");
     }
    }
    
    public class Hospital {
     public static void main(String args[]) {
      Surgeon s = new Surgeon();
      s.Doctor_Details();
      s.Surgeon_Details();
     }
    }
    Answered on February 3, 2019.
    Add Comment

    Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

    Answered on February 6, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.