Top 10 Java Inheritance Interview questions
1.what is inheritance?
1.Multiple inheritance( java doesn't support multiple inheritance).
2.Multilevel inheritance.
3.How Inheritance can be implemented in java?
2.implements
1.For Code Re usability.
2.For Method Overriding.
5.what is syntax of inheritance?
public class subclass extends superclass{
//all methods and variables declare here
}
6.what is multilevel inheritance?
8.How do you implement multiple inheritance in java?
interface A{
}
interface B{
}
class C extends interface A,B{
}
9.Can a class extend itself?
10.What happens if super class and sub class having same field name?
- inheritance is one of the oops concepts in java.inheritance is concept of getting properties of one class object to another class object.
- Inheritance represents the IS-A relationship,also known as parent-child relationship.
1.Multiple inheritance( java doesn't support multiple inheritance).
2.Multilevel inheritance.
3.How Inheritance can be implemented in java?
- Inheritance can be implemented in JAVA using below two keywords:
2.implements
- extends is used for developing inheritance between two classes and two interfaces.
- implements keyword is used to developed inheritance between interface and class.
1.For Code Re usability.
2.For Method Overriding.
5.what is syntax of inheritance?
public class subclass extends superclass{
//all methods and variables declare here
}
6.what is multilevel inheritance?
- Getting the properties from one class object to another class object level wise with different priorities.
- The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
- In multiple inheritance there is every chance of multiple properties of multiple objects with the same name available to the sub class object with same priorities leads for the ambiguity. also known as diamond problem. one class extending two super classes.
- Because of multiple inheritance there is chance of the root object getting created more than once.
- Always the root object i.e object of object class hast to be created only once.
- Because of above mentioned reasons multiple inheritance would not be supported by java.
- Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.
8.How do you implement multiple inheritance in java?
- Using interfaces java can support multiple inheritance concept in java. in java can not extend more than one classes, but a class can implement more than one interfaces.
interface A{
}
interface B{
}
class C extends interface A,B{
}
9.Can a class extend itself?
- No,A class can't extend itself.
10.What happens if super class and sub class having same field name?
- Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.