Inheritance Interview Questions and Answers


1.What is the purpose of ‘this’ keyword in java?
           In Java, ‘this’ keyword refers to current instance of the object. It is useful for differentiating between instance and local variables. It can be used to call constructors. Or it can be used to refer to the instance.
In case of method overriding, this is used for falling the method of current class.

2. Explain the concept of Inheritance?
            Inheritance is an important concept in OOPs. Some objects share certain characteristics and behavior. By using Inheritance, we can put the common behavior and characteristics in a base class which is also known as super class. Then all the objects with common behavior inherit from the same base class. It is also represented by IS-A relationship.
Inheritance promotes, code reuse, method overriding and polymorphism.


3. How To Describe Inheritance In Interview?
  1. Inheritance means one object acquires all the properties and behaviors of parent object.
  2. Inheritance is a compile-time mechanism.
  3. A super-class can have any number of subclasses. But a subclass can have only one superclass.
  4. The extends keyword indicates that you are making a new class that derives from an existing class.
  5. The superclass and subclass have “is-a” relationship between them.
  6. Inheritance is used For Method Overriding and For Code Reusability.

3. Which class in Java is super class of every other class?

          Java is an object oriented programming language. In Java, Object class is the superclass of every other class.

4. Why Java does not support multiple inheritance?
             Multiple Inheritance means that a class can inherit behavior from two or more than two parent classes. The issue with Multiple Inheritance is that both parent classes may have different implementation for the same method. So they have different ways of doing the same thing.  Now which implementation should the child class choose? This leads to ambiguity in Multiple Inheritance. This is the main reason to not supporting Multiple Inheritance implementation in Java. 
             Lets say you have a class TV and another class Atom Bomb. Both have method switchOn() but only TV has switchOff() method. If your class inherits from both these parent classes then you have an issue that you can switchOn() both parents, but switchOff will only switchOff() TV. But you can implement multiple interfaces in Java.

5. In OOPS, what is meant by composition?
              Composition is also known as “has-a” relationship. In composition, “has-a” relation relates two classes. E.g. Class Car has a steering wheel. If a class holds the instance of another class, then it is called composition.

6. How aggregation and composition are different concepts?
              In OOPS, Aggregation and Composition are the type of association relations. A composition is a strong relationship. If the composite object is destroyed, then all its parts are also destroyed. E.g. A Car has a Steering Wheel. If Car object is destroyed, then there is no meaning of the Steering Wheel. In Aggregation, the relationship is weaker than Composition. E.g. A Library has students. If a Library is destroyed, Students still exist. So Library and Student are related by Aggregation. A Library has Books. If Library is destroyed, the Books are also destroyed. Books of a Library cannot exist without the Library. So Book and Library are related by Composition.

7. Why there are no pointers in Java?
            In Java there are references instead of pointers. These references point to objects in memory. But there is no direct access to these memory location. JVM is free to move the objects within VM memory. The absence of pointers help to managing memory and garbage collection effectively in java. Also it provides developers with convenience of not getting worried about memory allocation and deallocation.


8. If there are no pointers in Java, then why do we get exception NullPointerException? 
            In Java, the pointer equivalent is Object reference. When we use a .it points to object reference. So JVM uses pointers but programmer only see object references.In case an object reference points to null object, and we try to access a method or member variable on it, then we get exception NullPointerException.

9. What is the purpose of ‘super’ keyword in java?
     The super keyword in java is a reference variable that is used to refer immediate parent class object.
     ‘super’ keyword is used in the methods or constructor of a child class. It refers to immediate parent class of an object.  Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.
Super keyword has three purposes :
  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.
10. Is it possible to use super() and this() both in same constructor?
               No, Java does not allow using both this() and super() in same constructor. As per Java specification, this() and super() must be the first statement in a constructor.

11.What is the meaning of object cloning in Java?
             Object.clone() method is used for creating an exact copy of object in Java. It acts like a copy constructor. It creates and returns copy of the object, with the same class and with all the fields having same values as of the original object. One disadvantage of cloning is that the return type is only Object. It has to be explicitly cast to actual type.

13. Types of Inheritance in Java?
There are 5 types of Inheritance.
  1. Single Inheritance: One class is extended by only one class.
  2. Multilevel Inheritance: One class is extended by a class and that class in turn is extended by another class thus forming a chain of inheritance.
  3. Hierarchical Inheritance: One class is extended by many classes.
  4. Hybrid Inheritance: It is a combination of above types of inheritance.
  5. Multiple Inheritance: One class extends more than one classes. (Java does not support multiple inheritance.)
14. Can a class extend more than one classes or does java support multiple inheritance? If not, why?
Java does not support multiple inheritance. This feature is avoided intentionally to avoid ambiguity, complexity and confusion.
For example, If Class C extends Class A and Class B which have a method with same name, then Class C will have two methods with same name.
This causes ambiguity and confusion for which method to use. To avoid this, java does not supports multiple inheritance.

15. What is Method Overriding And Method Hiding in Java?
Method Overriding:
An instance method in a subclass with the same signature (name, number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.
The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.
An overriding method can also return a subtype of the type returned by the overridden method. This subtype is called a covariant return type.
When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass.
If, for some reason, the compiler detects that the method does not exist in one of the superclasses, then it will generate an error.

Method Hiding:
If a subclass defines a static method with the same signature as a static method in the superclass, then the method in the subclass hides the one in the superclass.
The distinction between hiding a static method and overriding an instance method has important implications:
  1. The version of the overridden instance method that gets invoked is the one in the subclass.
  2. The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
16. How do you restrict a member of a class from inheriting to it’s sub classes?
By declaring that member as a private. Because, private members are not inherited to sub classes.

17. Are constructors and initializers also inherited to sub classes in Java?
No, Constructors and initializers(Static initializers and instance initializers) are not inherited to sub classes.
But, they are executed while instantiating a sub class.

18. Are static members inherited to sub classes in Java?
"Inherited" is not an ideal description of what is happening; a better way to describe it would be to say that static variables are shared among the subclasses of the base class.

All derived classes obtain access to static variables of their base classes. This includes protected variables, mirroring the situation with variables that are inherited.

The concept of hiding applies as well: when a class-specific variable str appears in the Child class, it hides the str variable of the parent class.

Note that the variable str of the base class does not become inaccessible: Child can still access it by fully qualifying with the name of Parent class.

public class Parent {
        static String str = "Parent";
    }

*Static variables shared among the subclasses of the base class as below:

public class Child extends Parent {
        public static void main(String [] args)
        {
            System.out.println(Child.str);
        }
    }
Output ==> Parent

** Hiding Concept applies as below:
 
public class Child extends Parent {
    static String str = "Child";
    public static void main(String [] args)
    {
        System.out.println(Child.str);
    }
}
output ==> Child

Comments