Inheritance in java
In this tutorial, we will see what is inheritance in Java and after that, we will discuss what is the possible java inheritance interview questions and answers.
Java is an object-oriented programming language and its support oops concept inheritance. Inheritance is a mechanism in which we can access the all properties of the parent class in the child class. for access, the properties of the parent class we use “extends” keyword.
Java inheritance example with source code:
class Child extends Parent { public static void main(String[] args) { Child obj = new Child(); System.out.println("Ans is "+obj.a); } } class Parent{ int a =10; }
Inheritance in java with interview questions and answers with example
Q) Types of inheritance in java
Class level inheritance :
- Single level
- Multilevel
- Hierarchical
Interface level inheritance:
- Multiple inheritance
- Hybrid inheritance
For better understanding or practical view of the type of inheritance please watch the video tutorial.
Q) Why multiple inheritances are not supported in java?
Let’s take a real-time example for this, class A and B extend class C. and there is a common method in class A and B the name of the method is “show()”. so compile will confuse between the classes.
So for achieving multiple inheritances in Java, we will use interface.
public class C implements A,B{ public static void main(String[] args) { C obj = new C(); obj.show(); } public void show() { // TODO Auto-generated method stub System.out.print("helllo"); } } interface A { public void show(); } interface B { public void show(); }
Q) How can be achieved multiple inheritances in java?
Using Interface.
public class C implements A,B{ public static void main(String[] args) { C obj = new C(); obj.show(); } public void show() { // TODO Auto-generated method stub System.out.print("helllo"); } } interface A { public void show(); } interface B { public void show(); }
Q) What is the super keyword in java?
super is a keyword in java and uses to reference the object of the immediate parent class.
- super can be used to refer immediate parent class instance variable.
- super can be used to invoke immediate parent class method.
- super() can be used to invoke immediate parent class constructor.
Q)What is Method Overriding in Java?
Method Overriding allow to implements the superclass method in child class. with the same name and same parameter. method overriding is used as a runtime polymorphism.
Q)Can we override a private method in Java?
NO, Since the method is private it is not visible to the other classes.Hence the derived class does not inherit this method.
Q)Can we override static method in Java?
No, We can not override the static method, because they are not part of the object’s state. Rather, they belong to the class.
Q)How Inheritance can be implemented in java?
Class A extends Class B.
Interface A extends Interface B.
Class A Implements Interface B.
Q)Why we need to use Inheritance?
Reuse once written code again and again.
Q)Can a class extend itself?
No, it is not possible. It does not make sense.