Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 20 September 2017

Interview Questions and Answers on Inheritance in Java

Java Inheritance Interview Questions

Java Inheritance Interview Questions and Answers

Now, Here we are going see some most important interview questions and answers on inheritance in java. This is the most important topic from the core java interview point of view.

There are many oops concepts interview questions are asked in core java interviews and Java inheritance concept is one of them.


(1) What is inheritance in java?

Inheritance is the oops concept and in java inheritance means re-usability. By using this inheritance concept a child class can inherit its parent class so that the child class can reuse all the field and methods of its parent class.


(2) What are the types of inheritance?

There are 5 types of inheritance. Suppose there is a class A, B, C, and D.

1) Single level inheritance.

B extends A

2) Multilevel inheritance.

C extends B and B extends A

3) Multiple inheritance.

C extends B and A

4) Hierarchical inheritance.

C  and B extends A  

5) Hybrid inheritance.

D extends C and B and C and B extends A


(3) Use of inheritance in java?

There are many reasons for using inheritance in java and these are given below.
  1. To achieve dynamic binding or method overriding.
  2. For code re-usability.
  3. To save time.

(4) Does java support multiple inheritance?

No, Java doesn't support multiple inheritance because of complexity and ambiguity in a program. Java does not support multiple inheritance through the class but it is possible through the interface.

(5) What is the syntax of inheritance?

There is the syntax of inheritance.

//Single levele inheritance
class A
{
//data member;
//member function
}
class B extends A
{
//data member;
//member function
}
}


(6) What is IS-A relationship in inheritance?

Java inheritance represents is-a relationship and parent-child relationship is also know as is-a relationship.


(7) What is aggregation in java?

If a class have an entity reference, it is known as aggregation and aggregation represents Has-a relationship.

For example 

class Student
{
String name;
Address  address;//Adderess is a class
}

Here Address is a class which may contain other information like city, country, etc.


(8) By default, all the classes extend which class in java?

In java, All the classes extend Object class by default because Object class is the super class in java.


(9) How inheritance can be implemented in java?

By two ways we can implement inheritance.

  • Using extends keyword.
  • Using implements keyword.

(10) How do you implement multiple inheritance in java?

We can implement multiple inheritance in java by using interface concept. A class can implement multiple interface at the same time but a class can't extend more than one class at a time.

For example:

interface My
{
//method
}
interface My1
{
//method
}
class Test implements My,My1
{}


(11) Can a class extend itself?

No, A class can't extend itself.


(12) Are interfaces extend Object class by default?

As we know that all the classes extend Object class by default but the interface does not extend Object class by default.


(13) Can you inherit a final class in java?

No, We can't extend the final class.


(14) Can you inherit final method of super class into a sub-class?

Yes, we can inherit final method of a super class into a sub-class.


(13) Can you inherit private members of super class into a sub-class?

No, we cannot inherit private member of a super class into a sub-class.


(14) Can we inherit constructor in sub-class?

No, we cannot inherit constructor in sub-class.


(15) What happens if super class and sub class having same field name?

Super class field will be hidden in the sub class but you can access hidden field of a super class by using 'super' keyword in the sub-class.


(16) Can you inherit static member into a sub-class.

Share:

3 comments:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate