Top 21 Java Inheritance Interview Questions in addition to Answers

Last calendar week I wrote virtually some skillful Java OOP concept questions as well as In this article, I am going to part some often asked Inheritance based Java Interview questions as well as answers. Inheritance is ane of the most of import Object oriented concepts along alongside Abstraction, Encapsulation, as well as Polymorphism. Since most pop languages are object oriented e.g. Java as well as C++, you lot volition e'er respect a duad of questions from OOP concepts, specially from Polymorphism as well as Inheritance. It's expected from a Java developers to know virtually these OOP concepts as well as receive got an agreement of when to usage them e.g. many times Composition is the meliorate alternative than Inheritance because of flexibility it offers but when it comes to leverage polymorphism of type, you lot receive got to usage Inheritance. In Java inheritance is supported yesteryear linguistic communication using extends as well as implements keyword.

In Java, a degree tin extend some other degree as well as implement interface but an interface tin entirely extend other interfaces. At the same time, Java doesn't back upward multiple inheritances exactly similar C++ does, but it does back upward multiple inheritances of type yesteryear allowing an interface to extend multiple interfaces. You volition run into many of such Inheritance based Java questions inwards this article.


BTW, if you lot are an absolute beginner as well as wants to larn Object-oriented programming inwards Java, I propose to accept a facial expression at Head First Object Oriented Analysis as well as Design, it's fun to read, slow to empathize as well as contains skillful examples to explicate fundamental concepts.




Frequently asked Java Inheritance Interview Questions as well as Answers

Here is my listing of some interview questions based upon Inheritance OOP concept asked inwards diverse Java interviews. It's rattling useful for beginners, freshers, as well as junior Java programmers, but intermediate Java developers tin every bit good produce goodness from some advanced questions shared inwards this article e.g. seat of multiple inheritances inwards Java later JDK 1.8 release, which allows you lot to write code within interface inwards the shape of default methods or explaining Liskov exchange principle.


Question 1: What is Inheritance inwards Java? (detailed answer)
Answer: Inheritance is an Object oriented characteristic which allows a degree to inherit demeanour as well as information from other class. For example, a degree Car tin extend basic characteristic of Vehicle degree yesteryear using Inheritance. One of the most intuitive examples of Inheritance inwards the existent the world is Father-Son relationship, where Son inherit Father's property. If you lot don't know, Inheritance is the quick way to perish rich :)


Question 2: What are different types of Inheritance supported yesteryear Java? (detailed answer)
Answer: Java supports unmarried Inheritance, multi-level inheritance as well as at some extent multiple inheritances because Java allows a degree to entirely extend some other class, but an interface inwards Java tin extend multiple inheritances.

 I am going to part some often asked Inheritance based Java Interview questions as well as  Top 21 Java Inheritance Interview Questions as well as Answers



Question 3: Why multiple Inheritance is non supported yesteryear Java? (detailed answer)
Answer: Java is introduced later C++ as well as Java designer didn't desire to accept some C++ characteristic which is confusing as well as non essential. They hollo upward multiple inheritances is ane of them which doesn't justify complexity as well as confusion it introduces. You tin every bit good cheque why multiple inheritances are non supported inwards Java for to a greater extent than reasons as well as intelligence exactly about this.


Question 4: Why Inheritance is used yesteryear Java Programmers? (detailed answer)
Answer: Inheritance is used for code reuse as well as leveraging Polymorphism yesteryear creating a type hierarchy. It's meliorate to usage Inheritance for type announcement but for code reuse composition is a meliorate alternative because it's to a greater extent than flexible. See this article for learning to a greater extent than virtually why Composition is meliorate than Inheritance.


Question 5: How to usage Inheritance inwards Java? (detailed answer)
Answer: You tin usage Inheritance inwards Java yesteryear extending classes as well as implementing interfaces. Java provides ii keywords extends as well as implements to scope inheritance.  A degree which is derived from some other degree is known every bit a subclass as well as an interface which is derived from some other interface is called subinterface. Influenza A virus subtype H5N1 degree which implements an interface is known every bit implementation.



Question 6: What is the syntax of Inheritance? (detailed answer)
Answer: You tin usage either extends of implements keyword to implement Inheritance inwards Java.  A degree extends some other degree using extends keyword, an interface tin extend some other interface using extend keyword, as well as a degree tin implement an interface using implements keyword inwards Java.


Question 7: What is the divergence betwixt Inheritance as well as Encapsulation? (detailed answer)
Answer: Inheritance is an object oriented concept which creates a parent-child relationship. It is ane of the ways to reuse the code written for nurture degree but it every bit good forms the footing of Polymorphism. On the other hand, Encapsulation is an object oriented concept which is used to enshroud the internal details of a degree e.g. HashMap encapsulate how to shop elements as well as how to calculate hash values.


Question 8: What is the divergence betwixt Inheritance as well as Abstraction? (detailed answer)
Answer: Abstraction is an object oriented concept which is used to only things yesteryear abstracting details. It helps inwards the designing system. On the other hand, Inheritance allows code reuse. You tin reuse the functionality you lot receive got already coded yesteryear using Inheritance. See Head First Object Oriented Analysis as well as Design for to a greater extent than details.

 I am going to part some often asked Inheritance based Java Interview questions as well as  Top 21 Java Inheritance Interview Questions as well as Answers



Question 9: What is the divergence betwixt Polymorphism as well as Inheritance? (detailed answer)
Answer: Both Polymorphism as well as Inheritance goes manus on hand, they assistance each other to scope their goal. Polymorphism allows flexibility, you lot tin select which code to run at runtime yesteryear overriding.  See the detailed reply for to a greater extent than details.


Question 10: What is the divergence betwixt Composition as well as Inheritance inwards OOP? (detailed answer)
Answer: One of the skillful query to cheque the candidate's object-oriented programming skills. There are several differences betwixt Composition as well as Inheritance inwards Java, some of them are following:

1. The Composition is to a greater extent than flexible because you lot tin modify the implementation at runtime yesteryear calling setXXX() method, but Inheritance cannot live changed i.e. you lot cannot enquire a degree to implement some other degree at runtime.

2. Composition builds HAS-A human relationship spell Inheritance builds IS-A human relationship e.g. Influenza A virus subtype H5N1 Room HAS Influenza A virus subtype H5N1 Fan, but Mango IS-A Fruit.

3. The parent-child human relationship is best represented using Inheritance but If you lot exactly desire to usage the services of some other degree usage Composition. For to a greater extent than differences run into 5 reasons to favor composition over Inheritance.

 I am going to part some often asked Inheritance based Java Interview questions as well as  Top 21 Java Inheritance Interview Questions as well as Answers



11. Can nosotros override static method inwards Java? (detailed answer)
No, you lot cannot override a static method inwards Java because it's resolved at compile time. In venture for overriding to work, a method should live virtual as well as resolved at runtime because objects are entirely available at runtime. This is ane of the tricky Java questions, where interviewer tries to confuse you. Influenza A virus subtype H5N1 programmer is never certain virtually whether they tin override or overload a static method inwards Java.


12. Can nosotros overload a static method inwards Java? (detailed answer)
Yes, you lot cannot overload a static method inwards Java. Overloading has zilch to produce alongside runtime but the signature of each method must live different. In Java, to modify the method signature, you lot must modify either publish of arguments, type of arguments or venture of arguments.


13. Can nosotros override a mortal method inwards Java? (detailed answer)
No,  you cannot override a mortal method inwards Java because the mortal method is non inherited yesteryear the subclass inwards Java, which is essential for overriding. In fact, a mortal method is non visible to anyone exterior the degree and, to a greater extent than importantly, a telephone yell upward to the mortal method is resolved at compile fourth dimension yesteryear using Type information every bit opposed to runtime yesteryear using the actual object.


Question 14: What is method hiding inwards Java? (detailed answer)
Answer: Since the static method cannot live overridden inwards Java, but if you lot declare the same static method inwards subclass hence that would enshroud the method from the superclass. It means, if you lot telephone yell upward that method from subclass hence the ane inwards the subclass volition live invoked but if you lot telephone yell upward the same method from superclass hence the ane inwards superclass volition live invoked. This is known every bit method hiding inwards Java.


Question 15: Can a degree implement to a greater extent than than ane interface inwards Java? (detailed answer)
Yes, Influenza A virus subtype H5N1 degree tin implement to a greater extent than than ane interface inwards Java e.g. Influenza A virus subtype H5N1 degree tin live both Comparable as well as Serializable at the same time. This is why the interface should live the best usage for defining Type every bit described inwards Effective Java. This characteristic allows ane degree to play a polymorphic role inwards the program.


Question 16: Can a degree extends to a greater extent than than ane degree inwards Java? (detailed answer)
Answer: No, a degree tin entirely extend exactly ane to a greater extent than degree inwards Java.  Though Every degree also, yesteryear default extend the java.lang.Object degree inwards Java.


Question 17: Can an interface extends to a greater extent than than ane interface inwards Java? (answer)
Answer: Yes, different classes, an interface tin extend to a greater extent than than ane interface inwards Java. There are several instance of this demeanour inwards JDK itself e.g. java.util.List interface extends both Collection as well as Iterable interface to tell that it is a Collection every bit good every bit it allows iteration via Iterator.


18: What volition come about if a degree extends ii interfaces as well as they both receive got a method alongside same refer as well as signature? (detailed answer)
In this case, a conflict volition arise because the compiler volition non able to link a method telephone yell upward due to ambiguity. You volition acquire a compile fourth dimension mistake inwards Java.


Question 19: Can nosotros transcend an object of a subclass to a method expecting an object of the super class? (answer)
Answer: Yes, you lot tin transcend that because subclass as well as superclass are related to each other yesteryear Inheritance which provides IS-A property.  I hateful Banana is a Fruit hence you lot tin transcend banana if somebody facial expression fruit. Now at that topographic point are scenario, where you lot can't produce e.g. when subclass violates the Liskov Substitution regulation i.e. you lot cannot transcend a plastic banana to someone expecting fruit :-), The eat() role volition throw exception.

 I am going to part some often asked Inheritance based Java Interview questions as well as  Top 21 Java Inheritance Interview Questions as well as Answers


Question 20: What is the Liskov exchange principle? (detailed answer)
Answer: The Liskov exchange regulation is ane of the 5 object-oriented pattern principles, collectively know every bit SOLID principles. This design principle is L of SOLID acronym. The Liskov exchange regulation states that inwards an object oriented plan if a role or method is expecting an object of base of operations degree hence  it should piece of occupation fine alongside a derived degree object every bit well. If it cannot role properly alongside derived degree object hence the derived degree is violating the Liskov Substitution principle.

For example, if a method is expecting a List you lot tin every bit good transcend ArrayList or LinkedList as well as it should piece of occupation exactly fine because ArrayList as well as LinkedList both follow Liskov Substitution Principle, but the java.sql.Date which is a subclass of java.util.Date inwards Java violates Liskov Substitution Principle because you lot cannot transcend an object of java.sql.Date degree to a method which is expecting an object of java.util.Date, Why? because all time-related method volition throw java.lang.UnsupportedOperationException.

Here is some other instance of violating The Liskov Substitution Principle, Square is a special type of Rectangle whose side yesteryear side sides are equal but making Square extending Rectangle violates LSP principle. For to a greater extent than details on SOLID pattern principles, read Clean Code yesteryear Rober C. Martin, the inventor of SOLID principles.

 I am going to part some often asked Inheritance based Java Interview questions as well as  Top 21 Java Inheritance Interview Questions as well as Answers




Question 21: How to telephone yell upward a method of a subclass, if you lot are holding an object of the subclass inwards a reference variable of type superclass? (answer)
Answer: You tin telephone yell upward a method of the subclass yesteryear offset casting the object concur yesteryear reference variable of  superclass into the subclass. Once you lot concur the object inwards subclass reference type, you lot tin telephone yell upward methods from the subclass. See how type casting plant inwards Java for to a greater extent than details.


That's all virtually some skillful interview questions based OOP concept,  Inheritance. You should every bit good know that how mortal as well as lastly variables comport upon Inheritance. How tin you lot extend a degree which is holding a mortal variable, as well as in all probability the difference betwixt mortal as well as protected modifier inwards Java? They are actually of import to empathize as well as usage Inheritance inwards Java.

Also, if you lot hollo upward nosotros receive got missed whatever of import query related to Inheritance o If you lot come upward across whatever skillful questions based upon Inheritance or other object oriented concept hence delight part alongside us.

Subscribe to receive free email updates:

0 Response to "Top 21 Java Inheritance Interview Questions in addition to Answers"

Posting Komentar