Java Interview Questions - Inheritance

1. How to access the member of a different class?

To access the member of different class user may use Aggregation or Inheritance.

2. What is Aggregation and advantages over Inheritance?

Aggregation is use to access the member of another class.

3. What is Inheritance and advantages over Aggregation?

To access the resource of super class inside the child class without creating object of each individual class is Inheritance.

4. What is the use of Inheritance?

Use of Inheritance is:

  1. To use Polymorphism.
  2. To promote code reuse.

5. Types of inheritance?

Inheritance is of 5 types. They are:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance
  5. Multiple Inheritance.

6. Why multiple inheritances not supported by class, its program?

Class never support Multiple Inheritance because it create ambiguity of members.

7. Define Multiple Inheritances?

When a class has more than one Super class is known as Multiple Inheritance.

  1. How Multiple inheritance are supported by interface?

All the methods of interface are public and abstract and user has to override within the sub class. Sub – class object always search the member within the sub –class first , and if member not present in sub class then it will search in super class. But in case of interface all the methods has to override in sub class before instantiating. So it never creates any ambiguity.

9. ProblemofInheritanceandAggregation,withprogram?

Major problem of Aggregation is user want to create object of each individual class for accessing the resource which is not possible each and every time in Aggregation.

10. Is there any limitation of using Inheritance?

Yes. When object create in the child class it create the object of all the super class.

11. Is there any other way that you can achieve inheritance in Java?

Please create an anonymous class of type implement parent interface, kindly override the method parent method ().

12. What are the drawbacks of inheritance?

When object create in the child class it create the object of all the super class.

13. I don’t want my class to be inherited by any other class. What should i do?

I have to declare the class as Final.

14. Which keywords use in Inheritance?

Two keyword s has special use in Inheritance. They are:

  1. Extend
  2. Implements…….

15. State the name of the class which is inherited and which is not inherited?

The class which is inherited is known as Super class and the class which is not inherited is known as Sub class.

16. Define Hierarchical Inheritance?

A more than one class belongs to a single class is Hierachical Inheritance.

17. How Multiple Inheritance support in java?

Multiple Inheritance support java by the help of Interface.

18. What “Extends’’ Keyword indicates?

Extends keyword indicates that you are making a new class that derives from an existing class.

19. Which type of relationship represent by Aggregation?

Aggregation represent HAS-A and IS-A relationship.

20. In which case Aggregation is the best choice?

Inheritance should be used only if the relationship IS-A is maintained throughout the lifetime of the objects involved otherwise Aggregation is the best choice.

21. Which class is the Super class of all class in java?

Object class.

22.Can an Interface extend another Interface and explain?

Yes.

23. What is the base class of all classes?

Java.lang.Object is the base class of all the classes.

24. Can you give few examples of final classes defined in Java API?

All the Wrapper classes including String, StringBuffered are the example of final class in java API.

25. How to access the member of default class?

To access the member of Default class user usegetClass () method and getSuperClass () method.

26. Who is the parent class of all the class in java?

Object is the parent class of all the class in java directly or indirectly.

27. What is the problem of aggregation?

Major problem of Aggregation is user want to create object of each individual class for accessing the resource which is not possible each and every time in Aggregation.