Inheritance

Inheritance

Introduction

  1. In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: a. subclass (child) - the class that inherits from another class b. superclass (parent) - the class being inherited from
  2. We use extends keyword for inheriting a parent class
  3. By default all classes will inherit from the object class if they do not have a parent class
  4. There are different types of inheritance a. multiple inheritance b. multilevel inheritance

a. multiple inheritances when two classes inherit from the same class (which is common ) example :

Multiple_Inheritance.jpg

b. multilevel inheritance is when a parent to child to grandparent example

multi.jpg

  1. We always create objects of the child class
  2. syntax: child_class extends parent_class
  3. Variable resolution: a. First it will check the local scope b. Then it will check in the instance scope c. Then it will check for parent class scope
  4. Inheritance is also called as IS A relationship

Types Of Method

  1. Specialized Methods: These are those methods that are only present in the child class
  2. Inherited Methods: These are those methods that are inherited by the child class and are present in the parent class
  3. Overridden Methods: These are those methods that are inherited by the child class from the parent class but are then changed in the child class

Creating Objects

  1. child c = new Child(): Accesses all members coming under child and parent classes
  2. Parent p = new Child(): Accesses the parent class members only, as well as the methods that have been overridden in the child class . Specialized methods still cannot be accessed

Downcasting & Upcasting

  1. By default parent class cannot access the specialized methods of children when we have an IS-A relationship
  2. Parent p = new Child(): Accesses the parent class members only, as well as the methods that have been overridden in the child class. Specialized methods still cannot be accessed
  3. Temporarily converting the parent class object to child type object is called downcasting
  4. After doing downcasting we can access children specialized methods as well
  5. On similar lines we can perform upcasting ....children to parent

Inheritance Example:

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
        calc object=new calc();
        System.out.println(object.add(10,20));
        System.out.println(object.subtract(20, 30));


    }
}



class calc extends subtraction{
    public int add(int a,int b){

        int result_add=a+b;
        return result_add;
    }


    public void child(){
        System.out.println("features only which child have");
    }




}


class subtraction {


        public int subtract(int a ,int b){
                return a-b;
        }

}

Parent Refernce child object

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
        subtraction object=new calc();
        System.out.println(object.add(10,20));
        System.out.println(object.subtract(20, 30));


    }
}



class calc extends subtraction{
    public int add(int a,int b){

        int result_add=a+b;
        return result_add;
    }


    public void child(){
        System.out.println("features only which child have");
    }




}


class subtraction {


        public int subtract(int a ,int b){
                return a-b;
        }

}

Error Generated

praent reference.png

Solving The Error Performing Downcasting

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
        subtraction object=new calc();
         calc downcasting=(calc)(object);

        System.out.println(downcasting.add(10,20));
        System.out.println(object.subtract(20, 30));


    }
}



class calc extends subtraction{
    public int add(int a,int b){

        int result_add=a+b;
        return result_add;
    }


    public void child(){
        System.out.println("features only which child have");
    }




}


class subtraction {


        public int subtract(int a ,int b){
                return a-b;
        }

}

Rules for overriding a method

  1. While overriding we cannot reduce the visibility of the access, that is if it is public in the parent and then we change its access to private then we will get an error
  2. Return type of the child and parent version of the overridden method should be the same
  3. Return type can be different if and only if the return type is covariant (return type should have IS-A relationship )
  4. Parameter list must also be the same for overridden methods
  5. For method overriding to happen compulsorily there should be inherited for instance methods

Static Methods

  1. They will participate in inheritance but not it overriding.Ths process is called as method hiding