Java Inheritance Interview Questions

JAVA

Java Inheritance Interview Questions

  1. Explain the use of ‘this’ keyword in java?

  2. Explain the concept of Inheritance?

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

  4. Why Java does not support multiple inheritance?

  5. What do you mean by composition?

  6. How aggregation and composition are different concepts?

  7. Why there are no pointers in Java?

  8. If there are no pointers in Java, then why do we get NullPointerException?

  9. Explain the use of ‘super’ keyword in java.

  10. Is it possible to use this() and super() both in the same constructor?

  11. What is the meaning of object cloning in Java?

  12. Use of a static variable?

  13. Why it is not a good practice to create static variables in Java?

  14. Use of a static method in Java.

  15. Why do we mark the main method as static in Java?

  16. In what scenario do we use a static block?

  17. Is it possible to execute a program without defining a main() method?

  18. What happens when the static modifier is not mentioned in the signature of the main method?

  19. Differentiate between a static method and instance method in Java?

ANSWERS / HINTS

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

2- Some objects share certain characteristics and behaviour using Inheritance, we can put the common behaviour and characteristics in a base class which also known as a superclass and then all the objects with common behaviour inherit from this base class. Inheritance promotes code reuse, method overriding and polymorphism.

3- In Java, Object class is the superclass of every other class.

4-  Multiple Inheritance means that a class can inherit behaviour from two or more parent classes. The issue with Multiple Inheritance is that both the 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. This is the main reason for Java not supporting Multiple Inheritance in implementation.

5- 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- Aggregation and Composition are types of association relations. A composition is a strong relationship. If the composite object is destroyed then all its parts are destroyed. For example, A Car has a Steering Wheel. If Car object is destroyed, then there is no meaning of Steering Wheel. In Aggregation, the relationship is weaker than Composition.

For example, 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- There are references instead of pointers. These references point to objects in memory. But there is no direct access to these memory locations. JVM is free to move the objects within VM memory. The absence of pointers helps Java in managing memory and garbage collection effectively. Also, it provides developers with the convenience of not getting worried about memory allocation and de-allocation.

8- In Java, the pointer equivalent is an Object reference. When we use a . it points to object reference.  So JVM uses pointers but programmers 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 NullPointerException.

9- ‘super’ keyword is used in the methods or constructor of a child class. It refers to the immediate parent class of an object. By using ‘super’ we can call a method of the parent class from the method of a child class. We can also call the constructor of a parent class from the constructor of a child class by using ‘super’ keyword.

10- No, Java does not allow using both super() and this() in the same constructor. As per Java specification, super() or this() must be the first statement in a constructor.

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

12- This variable is loaded in memory only once at the time of class loading. So it saves memory since it is not defined per object in Java.

13- Static variables are common to all the objects of a class. If a new object is created, there is no need to test the value of static variable. Any code that uses static variable can be in any state. It can be within a new object or at a class level. So the scope of static variable is open-ended in a Java class. If we want tighter control on scope, then variables should be created at the object creation level. Also defining static variables is not a good practice because they go against the principles of Object-Oriented Programming.

14- Java provides the feature of the static method to create behaviour at the class level. The static method is common to all the objects of a class.  We do not need to create an object of a class to call a static method. So it provides the convenience of not creating an object for calling it. Also, a static method can access and modify static data members. This also helps in keeping the behaviour as well as the state at the class level.

15- The main method in Java is marked as static so that JVM can call it to start the program. If the main method is not static, then which constructor will be called by Java process? As such it is known as a convention to mark the main method static in Java. But if we remove the static, then there will be ambiguity. Java process may not know which method of a class to call to start the program. So this convention helps in the Java process to identify the starting code for a program in class that is passed as an argument to the java process.

16- At times, there is a class that has static member variables. These variables need some complicated initialization. At this time static block helps as a tool to initialize complex static member variable initialization. The static block is executed even before the execution of main. Sometimes, we can also replace static block with a static method of class.

17- No, with Java 7 onwards, you need a main() method to execute a program. In earlier versions of Java, there was a workaround available to use static blocks for execution. But now this gap has been closed.

18- As per Java specification, main method has to be marked as static. It needs only one argument that is an array of strings. A program can compile with a non-static method. But on execution, it will give NoSuchMethodError.

19- Often, there is a need to define behaviour for a class that is not dependent on member variables of an object. Such behaviour is captured in a static method. If there is a behaviour dependent upon the member variables of an object, then we do not mark it static, it remains an instance method.

To call a static method, we do not need to create an object. We just call it with the class name. But to call an instance method, we need to create/get an object first. Instance member variables cannot be accessed by a static method. But an instance method can call both instance variables and static variables.

 

Check Interview Questions on Java

Wikipedia Java