Skip to main content

8. Inheritance

 

Inheritance

  • Inheritance is a mechanism that allows a class to inherit the properties and behaviors of another class.
  • The derived class inherits all the members of the base class and can also add its own members or override the inherited ones.
  • Every class we'll ever use or ever write will inherit from class Object (except of course class Object itself).
  • class ShellClass {
        public static void main(String[] args) {
            ShellClass t1 = new ShellClass();
            ShellClass t2 = new ShellClass();
            if (!t1.equals(t2))
                System.out.println("t1 and t2 instances are not same.");
            if (t1 instanceof Object)
                System.out.println("t1 is an instance of Object class.");
        }
    }

  • The two most common reasons to use inheritance are:
    • To promote code reuse.
    • To use polymorphism.

    IS-A and HAS-A Relationships

    • IS-A is a way of saying, "this thing is a type of that thing."
    • We express the IS-A relationship in Java through the keywords extends (for class inheritance) and implements (for interface implementation).
    • "Car extends Vehicle" means "Car IS-A Vehicle".
    • class Vehicle {
      }

      class Car extends Vehicle {
      }

    • HAS-A relationships are based on usage, rather than inheritance.
    • Class A HAS-A B if code in class A has a reference to an instance of class B.
    • class B {
      }

      class A {
          B refVar = new B();
      }

    Single Inheritance

    Subclass inherit the features of one superclass.

    class SuperClass {
    }

    class SubClass extends SuperClass {
    }

    Multilevel Inheritance

    A derived class will be inheriting a base class and as well as the derived class also act as the base class to other class.

    class BaseClass {
    }

    class IntermediatoryClass extends BaseClass {
    }

    class DerivedClass extends IntermediatoryClass {
    }

    Hierarchical Inheritance

    One class serves as a superclass for more than one subclass.

    class SuperClass {
    }

    class SubClass1 extends SuperClass {
    }

    class SubClass2 extends SuperClass {
    }

    class SubClass3 extends SuperClass {
    }

    Multiple Inheritance

    One class have more than one superclass and inherit features from all parent classes. Java doesn't support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through interfaces.

    interface A {
    }

    interface B {
    }

    interface C extends A, B {
    }

    Note: Some languages (like C++) allow a class to extend more than one other class. The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy.

    Hybrid Inheritance

    It is a mix of two or more of the above types of inheritance. Since Java doesn't support multiple inheritances with classes, hybrid inheritance is also not possible with classes.

    interface A {
    }

    interface B extends A {
    }

    interface C extends A {
    }

    interface D extends B, C {
    }

Prev Next

Comments