Java - Interview Questions and Answers for 'Inheritance' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Inheritance' - 28 question(s) found - Order By Newest

Frequently asked to fresh graduates and less experienced developers. Among the first few questions in many interviews.
  Q1. What are the difference between composition and inheritance in Java?Core Java
Admin
info@buggybread.com
Ans. Composition - has-a relationship between classes.
Inheritance - is-a relationship between classes.

Composition - Composing object holds a reference to composing classes 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   composition   inheritance vs composition     HCL Technologies  Tata Consultancy (TCS)  Infosys  Amazon  Source Intelligence  News UK  Starmount  MapQuest  SalesForce  Expedia  Deegit  JP Morgan  Canva  Todle  Yodle  Berkley Net  Help Scout  Partners HealthCare  Doximity  Commvault  Heroku  Starmount  Help.com  healthcare system  TU Kaiserslautern      basic        frequent

Try 5 Question(s) Test


 Q2. Why every object constructor automatically call super() in Object before its own constructors?Core Java
Admin
info@buggybread.com
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   inheritence     Itech      intermediate


 Q3. How compiler handles the exceptions in overriding ?Core Java
Admin
info@buggybread.com
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   oops


Frequently asked to fresh graduates and less experienced.
 Q4. Difference between Compositions and Inheritance ?Core Java
Admin
info@buggybread.com
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   composition   difference between   basic interview question      basic        frequent

Try 2 Question(s) Test


Frequently asked to fresh graduates and less experienced.
 Q5. Which of the following is tightly bound ? Inheritance or Composition ?Core Java
Anonymous
Ans. Inheritance.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inheritance   java   oops   composition     Deegit      basic        frequent


 Q6. Does a class inherit the constructor of its super class?
Admin
info@buggybread.com
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


 Q7. What will happen if class implement two interface having common method?
Admin
info@buggybread.com
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


 Q8. Can we compose the Parent Class object like this ?

class BuggyBread1 extends BuggyBread2 {

private BuggyBread2 buggybread2;

public static void main(String[] args){
buggybread2 = new BuggyBread2();
}
}
Admin
info@buggybread.com
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


 Q9. What are the different types of inheritance in Hibernate ?Hibernate
Admin
info@buggybread.com
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     Fidelity      Basic        frequent



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Can we extend an Enum ?Core Java
Admin
info@buggybread.com
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   yes-no     Sapient      intermediate


 Q11. Shouldn't we make a class with all static members is its just expected to be executed as a standalone program with just one thread. Moreover Lets assume that there is no runtime Polymorphism required and there is no need for serialization ?Design
Admin
info@buggybread.com
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.
  Q12. What is Inheritance ?Core Java

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  oops concepts  java concepts  code reuse  code re-use   classes  derived classes     Maverick Software  Infosys  HCL Technologies  Tata Consultancy (TCS)  Cognizant (CTS)  Myntra  Capital One  Barclays  Accenture  Infoview Technologies  Wipro      basic        frequent


  Q13. How is multiple inheritance implemented in Java ?Core Java

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   inheritance   oops concept     Credit Suisse  Amdocs  Ample Softwares  Tudip Technologies  Edifecs  VMWare  NSEiT  Exterro  theScore  BlackRock        frequent


 Q14. What is multilevel inheritance ?Core Java2016-06-23 13:52:50

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  inheritance  oops concepts     Charles Schwab  ZapBuild Technologies  Cardinal Health      basic

Try 1 Question(s) Test


 Q15. Does default methods introduce multiple inheritance and the Diamond problem in Java 8 ?Core Java2016-07-20 17:16:28

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  diamond problem   interfaces


 Q16. Can we access private members of the parent class ? i.e Are private members of the class visible in the derived class ?Core Java2016-08-04 09:24:34

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   members visibility      basic        frequent


 Q17. Difference between multiple and multi level inheritance ?Core Java2016-08-13 07:51:02

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   multiple inheritance   multi level inheritance      Basic        frequent


  Q18. Why java doesn't support multiple Inheritance ? or Explain Java Diamond Problem.Core Java2017-01-09 11:45:34

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  diamond problem     Sirius Computer Solutions  Hewlett Packard (HP)  Sun Trading  Amdocs  Ample Softwares  Tudip Technologies  Edifecs  VMWare  NSEiT  Credit Suisse  Exterro  theScore  BlackRock      basic        frequent


 Q19. How are the concept of Association related to Composition and Inheritance ?Core Java2017-02-06 16:18:04

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  inheritance  association  relationship between objects



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. What happens to private variables during inheritance ?Core Java2017-06-07 16:35:35

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


 Q21. Explain Inheritance and Polymorphism.Core Java2017-07-10 14:27:40

 This question was recently asked at 'Wipro'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inheritance  polymorphism     Wipro      Basic        frequent


 Q22. Is Runtime Polymorphism possible without Inheritance ?Core Java2017-08-06 19:12:26

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  method overriding  inheritance


 Q23. if classes B and C extends Class A, Which of the following initialization is correct ?Core Java
a. B b = new C();
b. C c = new B();
c. B b = new A();
d. A a = new B();

Ans.d. A a = new B();

 Q24. If class A implements X interface, and Class B implements Y interface, and X is the parent of Y, Which of the following initialization is correct ?Core Java
a. A a = new B();
b. Y y = new A();
c. X x = new B();
d. Y y = new A();

Ans.c. X x = new B();

 Q25. What is the relationship between Vehicle and Engine in this example ?

public class Vehicle {
   Enginer engine;
   public void move(){
      engine = new Engine();
      engine.start();
   }
}
Core Java
a. Composition ( Vehicle has a Engine )
b. Composition ( Engine has a Vehicle )
c. Inheritance ( Vehicle is a Engine )
d. Inheritance ( Engine is a Vehicle )

Ans.a. Composition ( Vehicle has a Engine )

 Q26. What is the relationship between Car and Vehicle in the following code ?

public class Car extends Vehicle{
   Engine engine;   
   
   public static void main(String[] args){
      Vehicle vehicle = new Car();
      car.move();
   }   

   public void move(){
      engine = new Engine();
      engine.start();
   }
}
Core Java
a. Composition ( Vehicle has a Car )
b. Composition ( Car has a Vehicle )
c. Inheritance ( Vehicle is a Car )
d. Inheritance ( Car is a Vehicle )

Ans.d. Inheritance ( Car is a Vehicle )

 Q27. What is the problem with the following code ?

public class Car extends Vehicle{
   Vehicle vehicle;
   
   Car(){
      super();
      this.vehicle = new Vehicle();
   }
}
Core Java
a. There is an Inheritance as well as Composition relationship between Vehicle and Car which is not permitted
b. We cannot initialize the parent class instance within the constructor
c. Call to super is illegal
d. There is no problem

Ans.d. There is no problem

 Q28. Which of the following is not true ?Core Java
a. A Java class can extend multiple classes
b. A Java class can implement multiple interfaces
c. A Java interface can extend multiple interfaces
d. All of above are true

Ans.a. A Java class can extend multiple classes



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: