Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| |||||||||||
Core Java - Interview Questions and Answers for 'Inheritance' - 26 question(s) found - Order By Newest | |||||||||||
Frequently asked to fresh graduates and less experienced developers. Among the first few questions in many interviews. | |||||||||||
| |||||||||||
Ans. Composition - has-a relationship between objects. Inheritance - is-a relationship between classes. Composition - Composing object holds a reference to composed objects and hence relationship is loosely bound. Inheritance - Derived object carries the base class definition in itself and hence its tightly bound. Composition - Used in Dependency Injection Inheritance - Used in Runtime Polymorphism Composition - Single class objects can be composed within multiple classes. Inheritance - Single class can only inherit 1 Class. Composition - Its the relationship between objects. Inheritance - Its the relationship between classes. | |||||||||||
Sample Code for inheritance Sample Code for composition | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   java concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   inheritance  object oriented programming (oops)  inheritance vs composition  object oriented programming (oops)  oops concepts Asked in 29 Companies basic   frequent | |||||||||||
Try 5 Question(s) Test | |||||||||||
| |||||||||||
Ans. Derived object carries the body of its class as well as the body of the parent class. Its body ( member elements ) is initialized using its own class constructor whereas the body ( member elements ) carried from the parent class are initialized using super class constructor. So In order to initialize the elements of the parent class before its own elements are even initialized, super is called. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   constructor   super   inheritance  object oriented programming (oops)  oops concepts   inheritence Asked in 1 Companies intermediate | |||||||||||
| |||||||||||
Ans. 1)The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method. 2)If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception 3) Different exceptions in java follow some hierarchy tree(inheritance). In this case , if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   overriding   exceptions   inheritence   inheritance  object oriented programming (oops)  oops concepts   oops | |||||||||||
Frequently asked to fresh graduates and less experienced. | |||||||||||
| |||||||||||
Ans. Inheritance means a object inheriting reusable properties of the base class. Compositions means that an abject holds other objects. In Inheritance there is only one object in memory ( derived object ) whereas in Composition , parent object holds references of all composed objects. From Design perspective - Inheritance is "is a" relationship among objects whereas Composition is "has a" relationship among objects. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   oops concepts   inheritance  object oriented programming (oops)  oops concepts   composition  object oriented programming (oops)  oops concepts   difference between   basic interview question Asked in 12 Companies basic   frequent | |||||||||||
Try 2 Question(s) Test | |||||||||||
Frequently asked to fresh graduates and less experienced. | |||||||||||
| |||||||||||
Ans. Inheritance. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  inheritance  object oriented programming (oops)  oops concepts   java   oops   composition  object oriented programming (oops)  oops concepts Asked in 1 Companies basic   frequent | |||||||||||
| |||||||||||
Ans. class A { void test() { System.out.println("test() method"); } } class B { void test() { System.out.println("test() method"); } } Suppose if Java allows multiple inheritance like this, class C extends A, B { } A and B test() methods are inheriting to C class. So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem Asked in 20 Companies basic   frequent | |||||||||||
| |||||||||||
Ans. No. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   constructor   inheritence   constructor inheritance   yes no | |||||||||||
Ans. Yes. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   composition   inheritance   yesno | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. No | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  abstract class   multiple inheritance  object oriented programming (oops)  oops concepts Asked in 2 Companies | |||||||||||
| |||||||||||
Ans. Yes, to a certain extent. But the objective for Final class could be beyond just enforcing composition as certain classes might have been created without inheritance or composition in mind. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  final class   composition  object oriented programming (oops)  oops concepts  inheritance  object oriented programming (oops)  oops concepts  composition  object oriented programming (oops)  oops concepts vs inheritance  object oriented programming (oops)  oops concepts expert | |||||||||||
| |||||||||||
Ans. 1. Enforcing composition over inheritance 2. Restricting overriding of certain methods 3. Final methods are faster than regular instance methods 4. Enforcing Immutability | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  final class   reasons for final class   restricting inheritance  object oriented programming (oops)  oops concepts expert | |||||||||||
| |||||||||||
Ans. That would not be a problem as both are specifying the contract that implement class has to follow. If class C implement interface A & interface B then Class C thing I need to implement print() because of interface A then again Class think I need to implement print() again because of interface B, it sees that there is already a method called test() implemented so it's satisfied. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   interfaces   multiple inheritance | |||||||||||
| |||||||||||
Ans. Table Per Class , Table per Sub Class , Table per Concrete Class | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  hibernate   orm   inheritance hibernate Asked in 1 Companies Basic   frequent | |||||||||||
| |||||||||||
Ans. No. Enums are final by design. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   enums   inheritance  object oriented programming (oops)  oops concepts   yes-no Asked in 1 Companies intermediate | |||||||||||
| |||||||||||
Ans. Still No in case we are making use of inheritance. we may have problem wherein we have program flow moving across common inherited method and specific methods of the derived class. call made to another static method in the parent class will only access the static class of the Parent class irrespective of the call from any of the derived class. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  static class   static vs singleton   inheritance | |||||||||||
Must know at all levels. Among Top 10 frequently asked questions in Java. Very frequently asked to fresh graduates or less experienced professionals. | |||||||||||
| |||||||||||
Ans. Its a facility for code reuse and independent extension wherein a derived class inherits the properties of parent class. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  inheritance  object oriented programming (oops)  oops concepts  oops concepts  java concepts  code reuse  code re-use   classes  derived classes Asked in 14 Companies basic   frequent | |||||||||||
| |||||||||||
Ans. There was no multiple inheritance in java before version 8 as implementing multiple interfaces cannot be termed as inheritance. With Java 8 , came the concept of default methods wherein the class implementing the interface carries the default implementation from the interface and hence facilitating multiple inheritance. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  multiple inheritance  object oriented programming (oops)  oops concepts   inheritance  object oriented programming (oops)  oops concepts   oops concept Asked in 10 Companies   frequent | |||||||||||
| |||||||||||
Ans. Multi Level Inheritance is multi level hierarchy of classes. Here is the example - http://www.buggybread.com/2015/09/java-se-class-diagram-classes-that_603.html Class RoleList extends ArrayList which in turn extends AbstractList which in turn extends AbstractCollection. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  multilevel inheritance  object oriented programming (oops)  oops concepts  inheritance  object oriented programming (oops)  oops concepts  oops concepts Asked in 3 Companies basic | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. Default methods results in multiple inheritance of behavior and not of state. In case we try to implement multiple interfaces with default method having same name and signature, and don't override it in implementation class, it will throw an error. For example - interface MyInterface { public void default myMethod(){ } } interface MyInterface2 { public void default myMethod(){ } } class MyClass implements MyInterface,MyInterface2 { } This code will compilation error "Duplicate Default Method" if we specify the definition of myMethod() in myClass, compiler won't complain and there is no conflict and MyClass can use overridden definition. But if we don't override myMethod() in MyClass, Java would be in conflict as to what definition should be carried to MyClass and hence throws compilation error. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  default methods  java 8  multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem   interfaces | |||||||||||
| |||||||||||
Ans. No, If both Parent and Derived are outer classes. public class Vehicle { private static String manufacturingDate = "2016"; } public class Car extends Vehicle{ public static void main(String args[]){ System.out.println(manufacturingDate); // error - The field Vehicle.manufacturingDate is not visible } } Yes, If derived is the inner class of Parent. public class Vehicle { private static String manufacturingDate = "2016"; static public class Car extends Vehicle{ public static void main(String args[]){ System.out.println(manufacturingDate); // no problem } } } | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  private members   private methods   private variables   inheritance  object oriented programming (oops)  oops concepts   members visibility   inner classes  nexted classes basic   frequent | |||||||||||
| |||||||||||
Ans. Multiple Inheritance refers to the concept of a class inheriting multiple classes. Example - Class C extends Class A ,Class B. This is not allowed in Java. Multilevel Inheritance refers to the concept of Inheritance in a chain. Example - Class B extends Class A, Class C extends Class B. This is permitted in Java. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  inheritance  object oriented programming (oops)  oops concepts   multiple inheritance  object oriented programming (oops)  oops concepts   multi level inheritance  object oriented programming (oops)  oops concepts Basic   frequent | |||||||||||
| |||||||||||
Ans. Composition and Inheritance are the different types of Associations for Classes. Composition is a has-a association between classes. Inheritance is a is-a association between classes. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  composition  object oriented programming (oops)  oops concepts  inheritance  object oriented programming (oops)  oops concepts  association  relationship between objects | |||||||||||
| |||||||||||
Ans. Private variables are not inherited. They are simply ignored and not made part of the derived object body. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  private variables  inheritance  object oriented programming (oops)  oops concepts | |||||||||||
| |||||||||||
Ans. Getting parent class object properties into child class is called Inheritance. The process of representing one form into multiple forms is called polymorphism. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  inheritance  object oriented programming (oops)  oops concepts  polymorphism  object oriented programming (oops)  oops concepts Asked in 1 Companies Basic   frequent | |||||||||||
| |||||||||||
Ans. Yes, Runtime Polymprohism requires either Class inheritance or Interface implementation. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  runtime polymorphism  object oriented programming (oops)  oops concepts  method overriding  inheritance  object oriented programming (oops)  oops concepts | |||||||||||
| |||||||||||
Ans. No | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  inheritance  object oriented programming (oops)  oops concepts | |||||||||||
| |||||||||||
| |||||||||||
| |||||||||||
| |||||||||||