Sunday, November 24, 2019

Top 21 Coffee Inheritance Interview Questions As Well As Answers

Last calendar week I wrote well-nigh some practiced Java OOP concept questions too In this article, I am going to portion some oft asked Inheritance based Java Interview questions too answers. Inheritance is i of the most of import Object oriented concepts along alongside Abstraction, Encapsulation, too Polymorphism. Since most pop languages are object oriented e.g. Java too C++, you lot volition ever notice a yoke of questions from OOP concepts, specially from Polymorphism too Inheritance. It's expected from a Java developers to know well-nigh these OOP concepts too receive got an agreement of when to utilization them e.g. many times Composition is the ameliorate pick than Inheritance because of flexibility it offers but when it comes to leverage polymorphism of type, you lot receive got to utilization Inheritance. In Java inheritance is supported past times linguistic communication using extends too implements keyword.

In Java, a aeroplane tin extend some other aeroplane too implement interface but an interface tin solely 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 past times allowing an interface to extend multiple interfaces. You volition encounter many of such Inheritance based Java questions inwards this article.


BTW, if you lot are an absolute beginner too wants to acquire Object-oriented programming inwards Java, I advise to accept a aspect at Head First Object Oriented Analysis too Design, it's fun to read, slow to empathise too contains practiced examples to explicate telephone exchange concepts.




Frequently asked Java Inheritance Interview Questions too 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, too junior Java programmers, but intermediate Java developers tin likewise produce goodness from some advanced questions shared inwards this article e.g. seat of multiple inheritances inwards Java afterwards JDK 1.8 release, which allows you lot to write code within interface inwards the cast of default methods or explaining detailed answer)
Answer: Inheritance is an Object oriented characteristic which allows a aeroplane to inherit behaviour too information from other class. For example, a aeroplane Car tin extend basic characteristic of Vehicle aeroplane past times using Inheritance. One of the most intuitive examples of Inheritance inwards the existent globe 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 past times Java? (detailed answer)
Answer: Java supports unmarried Inheritance, multi-level inheritance too at some extent multiple inheritances because Java allows a aeroplane to solely extend some other class, but an interface inwards Java tin extend multiple inheritances.

detailed answer)
Answer: Java is introduced afterwards C++ too Java designer didn't desire to accept some C++ characteristic which is confusing too non essential. They retrieve multiple inheritances is i of them which doesn't justify complexity too confusion it introduces. You tin likewise cheque why multiple inheritances are non supported inwards Java for to a greater extent than reasons too give-and-take roughly this.


Question 4: Why Inheritance is used past times Java Programmers? (detailed answer)
Answer: Inheritance is used for code reuse too leveraging Polymorphism past times creating a type hierarchy. It's ameliorate to utilization Inheritance for type annunciation but for code reuse composition is a ameliorate choice because it's to a greater extent than flexible. See this article for learning to a greater extent than well-nigh why Composition is ameliorate than Inheritance.


Question 5: How to utilization Inheritance inwards Java? (detailed answer)
Answer: You tin utilization Inheritance inwards Java past times extending classes too implementing interfaces. Java provides 2 keywords extends too implements to accomplish inheritance.  A aeroplane which is derived from some other aeroplane is known equally a subclass too an interface which is derived from some other interface is called subinterface. Influenza A virus subtype H5N1 aeroplane which implements an interface is known equally implementation.



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


Question 7: What is the departure betwixt Inheritance too Encapsulation? (detailed answer)
Answer: Inheritance is an object oriented concept which creates a parent-child relationship. It is i of the ways to reuse the code written for nurture aeroplane but it likewise forms the footing of Polymorphism. On the other hand, Encapsulation is an object oriented concept which is used to cover the internal details of a aeroplane e.g. HashMap encapsulate how to shop elements too how to calculate hash values.


Question 8: What is the departure betwixt Inheritance too Abstraction? (detailed answer)
Answer: Abstraction is an object oriented concept which is used to exactly things past times 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 past times using Inheritance. See Head First Object Oriented Analysis too Design for to a greater extent than details.

 I am going to portion some oft asked Inheritance based Java Interview questions too  Top 21 Java Inheritance Interview Questions too Answers



Question 9: What is the departure betwixt Polymorphism too Inheritance? (detailed answer)
Answer: Both Polymorphism too Inheritance goes paw on hand, they assist each other to accomplish their goal. Polymorphism allows flexibility, you lot tin guide which code to run at runtime past times overriding.  See the detailed respond for to a greater extent than details.


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

1. The Composition is to a greater extent than flexible because you lot tin alter the implementation at runtime past times calling setXXX() method, but Inheritance cannot live changed i.e. you lot cannot enquire a aeroplane to implement some other aeroplane 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 utilization the services of some other aeroplane utilization Composition. For to a greater extent than differences encounter 5 reasons to favor composition over Inheritance.

 I am going to portion some oft asked Inheritance based Java Interview questions too  Top 21 Java Inheritance Interview Questions too 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 social club for overriding to work, a method should live virtual too resolved at runtime because objects are solely available at runtime. This is i of the tricky Java questions, where interviewer tries to confuse you. Influenza A virus subtype H5N1 programmer is never certain well-nigh 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 tin overload a static method inwards Java. Overloading has aught to produce alongside runtime but the signature of each method must live different. In Java, to alter the method signature, you lot must alter either pose out of arguments, type of arguments or social club of arguments.


13. Can nosotros override a somebody method inwards Java? (detailed answer)
No,  you cannot override a somebody method inwards Java because the somebody method is non inherited past times the subclass inwards Java, which is essential for overriding. In fact, a somebody method is non visible to anyone exterior the aeroplane and, to a greater extent than importantly, a telephone phone to the somebody method is resolved at compile fourth dimension past times using Type information equally opposed to runtime past times 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 cover the method from the superclass. It means, if you lot telephone phone that method from subclass hence the i inwards the subclass volition live invoked but if you lot telephone phone the same method from superclass hence the i inwards superclass volition live invoked. This is known equally method hiding inwards Java.


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


Question 16: Can a aeroplane extends to a greater extent than than i aeroplane inwards Java? (detailed answer)
Answer: No, a aeroplane tin solely extend exactly i to a greater extent than aeroplane inwards Java.  Though Every aeroplane also, past times default extend the java.lang.Object aeroplane inwards Java.


Question 17: Can an interface extends to a greater extent than than i interface inwards Java? (answer)
Answer: Yes, dissimilar classes, an interface tin extend to a greater extent than than i interface inwards Java. There are several illustration of this behaviour inwards JDK itself e.g. java.util.List interface extends both Collection too Iterable interface to tell that it is a Collection equally good equally it allows iteration via Iterator.


18: What volition move on if a aeroplane extends 2 interfaces too they both receive got a method alongside same refer too signature? (detailed answer)
In this case, a conflict volition arise because the compiler volition non able to link a method telephone phone due to ambiguity. You volition acquire a compile fourth dimension fault inwards Java.


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

 I am going to portion some oft asked Inheritance based Java Interview questions too  Top 21 Java Inheritance Interview Questions too Answers


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

For example, if a method is expecting a List you lot tin likewise overstep ArrayList or LinkedList too it should piece of employment exactly fine because ArrayList too 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 overstep an object of java.sql.Date aeroplane 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 illustration of violating The Liskov Substitution Principle, Square is a special type of Rectangle whose next 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 past times Rober C. Martin, the inventor of SOLID principles.

 I am going to portion some oft asked Inheritance based Java Interview questions too  Top 21 Java Inheritance Interview Questions too Answers




Question 21: How to telephone phone 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 phone a method of the subclass past times start casting the object concur past times reference variable of  superclass into the subclass. Once you lot concur the object inwards subclass reference type, you lot tin telephone phone methods from the subclass. See how type casting plant inwards Java for to a greater extent than details.


That's all well-nigh some practiced interview questions based OOP concept,  Inheritance. You should likewise know that how somebody too lastly variables comport on Inheritance. How tin you lot extend a aeroplane which is holding a somebody variable, too belike the difference betwixt somebody too protected modifier inwards Java? They are actually of import to empathise too utilization Inheritance inwards Java.

Also, if you lot retrieve nosotros receive got missed whatsoever of import enquiry related to Inheritance o If you lot come upward across whatsoever practiced questions based upon Inheritance or other object oriented concept hence delight portion alongside us.

Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming inwards Java
Java - Object Oriented Programming [For Absolute Beginners]


No comments:

Post a Comment