Inheritance


Inheritance is an object-oriented programming concept where a new class, known as a subclass or derived class, inherits the properties and behaviors (attributes and methods) from another class, known as the superclass or base class. Inheritance allows for code reusability and the creation of a hierarchical relationship between classes.

IS-A Relationship

The "IS-A" relationship in inheritance represents a relationship between two classes where one class is a specialized form of another. Example

"Dog" IS-A "Animal". The subclass "Dog" inherits properties from the superclass "Animal", but may have additional specific characteristics.
        
        class Animal {
            void eat() {
                System.out.println("Animal is eating");
            }
        }
    
        class Dog extends Animal {
            void bark() {
                System.out.println("Dog is barking");
            }
        }
    
        public class Main {
            public static void main(String[] args) {
                Dog dog = new Dog();
                dog.eat();  // Inherited from Animal
                dog.bark(); // Specific to Dog
            }
        }
        
    
Types of Inheritance
Implementation of All Types of Inheritance in a Single Class
    // Base class (Super class)
    class Animal {
        void eat() {
            System.out.println("Animal is eating");
        }
    }
    
    // Single Inheritance: Dog extends Animal
    class Dog extends Animal {
        void bark() {
            System.out.println("Dog is barking");
        }
    }
    
    // Multi-level Inheritance: Dog extends Animal and Puppy extends Dog
    class Puppy extends Dog {
        void weep() {
            System.out.println("Puppy is weeping");
        }
    }
    
    // Hierarchical Inheritance: Dog and Cat both extends Animal
    class Cat extends Animal {
        void meow() {
            System.out.println("Cat is meowing");
        }
    }
    
    // Multiple Inheritance via Interface
    interface Pet {
        void play();
    }
    
    class DomesticDog extends Dog implements Pet {
        public void play() {
            System.out.println("Dog is playing");
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            // Single Inheritance
            Dog dog = new Dog();
            dog.eat();
            dog.bark();
    
            // Multi-level Inheritance
            Puppy puppy = new Puppy();
            puppy.eat();
            puppy.bark();
            puppy.weep();
    
            // Hierarchical Inheritance
            Cat cat = new Cat();
            cat.eat();
            cat.meow();
    
            // Multiple Inheritance via Interface
            DomesticDog domesticDog = new DomesticDog();
            domesticDog.play();
            domesticDog.bark();
            domesticDog.eat();
        }
    }
Advantages of Inheritance
Tricky Interview Questions and Answers on Inheritance
Question 1: Can a subclass inherit private members from a superclass?

Answer: No, private members of a superclass are not accessible directly by the subclass. However, they can be accessed through public or protected methods of the superclass.

Question 2: What is the difference between overriding and overloading in inheritance?

Answer: Overriding refers to redefining a method in the subclass that already exists in the superclass with the same signature. Overloading refers to having multiple methods in the same class with the same name but different parameter lists.

Question 3: Why does Java not support multiple inheritance?

Answer: Java doesn't support multiple inheritance using classes to avoid ambiguity caused by the "diamond problem." Instead, multiple inheritance is achieved using interfaces.

Question 4: Can constructors be inherited in Java?

Answer: No, constructors are not inherited. However, the subclass can call the constructor of the superclass using the super() keyword.

Question 5: How can we prevent inheritance in Java?

Answer: To prevent inheritance, the class can be declared as final. A final class cannot be subclassed.

Question 6: If a superclass has a method that is not overridden by a subclass, which method will be called when invoking that method on an object of the subclass?

Answer: The method from the superclass will be called, as the subclass has not provided its own overriding implementation.

Question 7: Consider a scenario where class A is extended by class B, and class B is extended by class C. If class C does not override a method from class A, which version of the method is called when an object of class C invokes it?

Answer: The method from class A will be called, as class B and class C do not override it.

Question 8: Can a subclass access protected members of a superclass?

Answer: Yes, protected members are accessible to subclasses and classes in the same package.

Question 9: How does inheritance affect memory management?

Answer: Objects of the subclass hold a reference to the parent class, meaning all fields from the superclass are also stored in memory when an object of the subclass is created. This hierarchical allocation can impact memory usage.