Java Inheritance Interview Questions & Answers

4 avg. rating (80% score) - 1 votes

Java Inheritance Interview Questions & Answers

Do you like to switch your career? Here's our recommendation on the important things to need to prepare for the job interview to achieve your career goals in an easy way. Java Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in java by which one class is allowed to inherit the features (fields and methods) of another class. The subclass can add its own fields and methods in addition to the superclass fields and methods. One can check the availability of the job across cities including Mumbai, Delhi, Bangalore, Pune and Hyderabad. Follow Wisdomjobs page for Java Inheritance job interview questions and answers page to get through your job interview successfully in first attempt.

Java Inheritance Interview Questions

Java Inheritance Interview Questions
    1. Question 1. What Is Inheritance In Java?

      Answer :

      Inheritance is an Object oriented feature which allows a class to inherit behavior and data from other class. For example, a class Car can extend basic feature of Vehicle class by using Inheritance. One of the most intuitive examples of Inheritance in the real world is Father-Son relationship, where Son inherit Father's property. If you don't know, Inheritance is the quick way to become rich.

    2. Question 2. Why Multiple Inheritance Is Not Supported By Java?

      Answer :

      Java is introduced after C++ and Java designer didn't want to take some C++ feature which is confusing and not essential. They think multiple inheritances is one of them which doesn't justify complexity and confusion it introduces. You can also check why multiple inheritances are not supported in Java for more reasons and discussion around this.

    3. Question 3. Why Inheritance Is Used By Java Programmers?

      Answer :

      Inheritance is used for code reuse and leveraging Polymorphism by creating a type hierarchy. It's better to use Inheritance for type declaration but for code reuse composition is a better option because it's more flexible. See this article for learning more about why Composition is better than Inheritance.

    4. Question 4. How To Use Inheritance In Java?

      Answer :

      You can use Inheritance in Java by extending classes and implementing interfaces. Java provides two keywords extends and implements to achieve inheritance.  A class which is derived from another class is known as a subclass and an interface which is derived from another interface is called subinterface. A class which implements an interface is known as implementation.

    5. Question 5. What Is The Syntax Of Inheritance?

      Answer :

      You can use either extends of implements keyword to implement Inheritance in Java.  A class extends another class using extends keyword, an interface can extend another interface using extend keyword, and a class can implement an interface using implements keyword in Java.

    6. Question 6. What Is The Difference Between Inheritance And Encapsulation?

      Answer :

      Inheritance is an object oriented concept which creates a parent-child relationship. It is one of the ways to reuse the code written for parent class but it also forms the basis of Polymorphism. On the other hand, Encapsulation is an object oriented concept which is used to hide the internal details of a class e.g. HashMap encapsulate how to store elements and how to calculate hash values.

    7. Question 7. What Is The Difference Between Polymorphism And Inheritance?

      Answer :

      Both Polymorphism and Inheritance goes hand on hand, they help each other to achieve their goal. Polymorphism allows flexibility, you can choose which code to run at runtime by overriding.  See the detailed answer for more details.

    8. Question 8. Can We Override Static Method In Java?

      Answer :

      No, you cannot override a static method in Java because it's resolved at compile time. In order for overriding to work, a method should be virtual and resolved at runtime because objects are only available at runtime. This is one of the tricky Java questions, where interviewer tries to confuse you. A programmer is never sure about whether they can override or overload a static method in Java.

    9. Question 9. Can We Overload A Static Method In Java?

      Answer :

      Yes, you cannot overload a static method in Java. Overloading has nothing to do with runtime but the signature of each method must be different. In Java, to change the method signature, you must change either number of arguments, type of arguments or order of arguments.

    10. Question 10. Can We Override A Private Method In Java?

      Answer :

      No,  you cannot override a private method in Java because the private method is not inherited by the subclass in Java, which is essential for overriding. In fact, a private method is not visible to anyone outside the class and, more importantly, a call to the private method is resolved at compile time by using Type information as opposed to runtime by using the actual object.

    11. Question 11. What Is Method Hiding In Java?

      Answer :

      Since the static method cannot be overridden in Java, but if you declare the same static method in subclass then that would hide the method from the superclass. It means, if you call that method from subclass then the one in the subclass will be invoked but if you call the same method from superclass then the one in superclass will be invoked. This is known as method hiding in Java.

    12. Question 12. Can A Class Implement More Than One Interface In Java?

      Answer :

      Yes, A class can implement more than one interface in Java e.g. A class can be both Comparable and Serializable at the same time. This is why the interface should be the best use for defining Type as described in Effective Java. This feature allows one class to play a polymorphic role in the program.

    13. Question 13. Can A Class Extends More Than One Class In Java?

      Answer :

      No, a class can only extend just one more class in Java. Through Every class also, by default extend the java.lang.Object class in Java.

    14. Question 14. Can An Interface Extends More Than One Interface In Java?

      Answer :

      Yes, unlike classes, an interface can extend more than one interface in Java. There are several example of this behavior in JDK itself e.g. java.util.List interface extends both Collection and Iterable interface to tell that it is a Collection as well as it allows iteration via Iterator.

    15. Question 15. What Will Happen If A Class Extends Two Interfaces And They Both Have A Method With Same Name And Signature?

      Answer :

      In this case, a conflict will arise because the compiler will not able to link a method call due to ambiguity. You will get a compile time error in Java.

    16. Question 16. What Is The Liskov Substitution Principle?

      Answer :

      • The Liskov substitution principle is one of the five object-oriented design principles, collectively know as SOLID principles. This design principle is L of SOLID acronym. The Liskov substitution principle states that in an object oriented program if a function or method is expecting an object of base class then  it should work fine with a derived class object as well. If it cannot function properly with derived class object then the derived class is violating the Liskov Substitution principle.
      • For example, if a method is expecting a List you can also pass ArrayList or LinkedList and it should work just fine because ArrayList and LinkedList both follow Liskov Substitution Principle, but the java.sql.Date which is a subclass of java.util.Date in Java violates Liskov Substitution Principle because you cannot pass an object of java.sql.Date class to a method which is expecting an object of java.util.Date, Why? because all time-related method will throw java.lang.UnsupportedOperationException.
      • Here is another example of violating The Liskov Substitution Principle, Square is a special type of Rectangle whose adjacent sides are equal but making Square extending Rectangle violates LSP principle. For more details on SOLID design principles, read Clean Code by Rober C. Martin, the inventor of SOLID principles.

    17. Question 17. How To Call A Method Of A Subclass, If You Are Holding An Object Of The Subclass In A Reference Variable Of Type Superclass?

      Answer :

      • You can call a method of the subclass by first casting the object hold by reference variable of  superclass into the subclass. Once you hold the object in subclass reference type, you can call methods from the subclass. See how type casting works in Java for more details.
      • That's all about some good interview questions based OOP concept,  Inheritance. You should also know that how private and final variables affect Inheritance. How can you extend a class which is holding a private variable, and probably the difference between private and protected modifier in Java? They are really important to understand and use Inheritance in Java.
      • Also, if you think we have missed any important question related to Inheritance o If you come across any good questions based upon Inheritance or other object oriented concept then please share with us.

Core Java Tutorial

Java Inheritance Practice Tests


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Tutorial