1. What is Inheritance in Java?
Inheritance means one class can inherit/acquires all of the properties and
behaviors from another class. Inheritance is a compile-time mechanism.
A super-class can have any number of subclasses. But a subclass can have only
one superclass.
The extends keyword indicates that you are making a new class that derives
from an existing class.
The superclass and subclass have “is-a” relationship between them.
Inheritance is used for Method Overriding and Code Reusability.
2. What is super class and subclass?
A class from where a subclass inherits features is called superclass. It is also
called base class or parent class.
A class that inherits all the members (fields, method) from other class is called
subclass. It is also called a derived class, child class, or extended class.
3. Which class in Java is superclass of every other class?
Ans: Object class is the superclass of every other class in Java.
4. Can we extend (inherit) final class?
Ans: No, a class declared with final keyword cannot be inherited.
5. Can a class extend more than one class?
Ans: No, one class can extend only a single class.
6. Can we inherit private members of base class to its subclass?
Ans: No
7. What are the different types of inheritance in Java?
a. Single inheritance
b. Multi-level inheritance
c. Hierarchical inheritance
d. Multiple inheritance
e. Hybrid inheritance
Single inheritance:
One class is extended by only one class i.e. creating a single subclass from a
single superclass is called single inheritance.
// Create a base class or superclass.
public class A
{
// Declare an instance method.
public void methodA()
{ System.out.println("Base class method"); }
}
// Declare a derived class or subclass and extends class A.
public class B extends A
{
public void methodB()
{ System.out.println("Child class method"); }
}
public class Myclass
{
public static void main(String[] args)
{
// Create an object of class B.
B obj = new B();
obj.methodA(); // methodA() of B will be called because by default, it is available in B.
obj.methodB(); // methodB() of B will be called.
}
}
Output:
Base class method
Child class method
Multilevel Inheritance:
A class that is extended by an another class and that class is extended by another
class forming chain inheritance is called multilevel inheritance in java.
package multilevelInheritance;
public class X
{
public void methodX()
{ System.out.println("Class X method"); }
}
public class Y extends X
{
public void methodY()
{ System.out.println("Class Y method"); }
}
public class Z extends Y
{
public void methodZ()
{ System.out.println("Class Z method"); }
public static void main(String[] args)
{
Z z = new Z();
z.methodX(); // Calling X grand class method.
z.methodY(); // Calling Y parent class method.
z.methodZ(); // Calling Z class local method.
}
}
Output:
Class X method
Class Y method
Class Z method
Hierarchical inheritance:
When one class is extended by many subclasses, it is known as hierarchical
inheritance.
package hierarchicalInheritanceEx;
public class A
{
public void msgA()
{ System.out.println("Method of class A"); }
}
public class B extends A
{
// Empty class B, inherits msgA of parent class A.
}
public class C extends A
{
// Empty class C, inherits msgA of parent class A.
}
public class D extends A
{
// Empty class D, inherits msgA of parent class A.
}
public class MyClass
{
public static void main(String[] args)
{
// Create the object of class B, class C, and class D.
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
// Calling inherited function from the base class.
obj1.msgA();
obj2.msgA();
obj3.msgA();
}
}
Output:
Method of class A
Method of class A
Method of class A
Multiple inheritance:
Deriving subclasses from more than one superclass is known as multiple inheritance.
Hybrid inheritance:
A hybrid inheritance is a combination of single and multiple inheritance.
8. Why multiple inheritance is not supported in java through class?
Multiple inheritance leads to confusion for the programmer. Consider a scenario as
shown in the below diagram. In this scenario, there are three classes A, B, and C. Class
C extends two parent classes such as class A and class B. Suppose class A and class B
have the same method msg() (say) having different implementations.
As per inheritance concepts, both methods will be inherited to class C. If we create an
object of class C and calls the method msg() from the child class object, which msg()
method will get called? (i.e. which copy of msg() method will available in C?)
So, there will create ambiguity and confusion to call the method msg from A or B class.
To reduce the ambiguity, complexity, and confusion, Java does not support multiple
inheritance through classes hence in Java one class cannot extend more than one class
simultaneously.
9. How does Multiple inheritance implement in Java?
Ans: Multiple inheritance can be implemented in Java by using interfaces. A class
cannot extend more than one class but a class can implement more than one interface.
10. Can we access subclass members if we create an object of superclass?
Ans: No, we can access only superclass members but not the subclass members.
11.Can we access both superclass and subclass members if we create an
object of subclass?
Ans: Yes, we can access both superclass and subclass members.
12.How will you restrict a member of a class from inheriting its subclass?
Ans: We can restrict members of a class by declaring them private because the
private members of superclass are not available to the subclass directly. They are
only available in their own class.
13. What happens if both superclass & subclass have a field with the same name?
Ans: Only subclass members are accessible if an object of subclass is instantiated
14. Will the code successfully compile? If yes, what is the output?
public class A {
int x = 20;
}
public class B extends A {
int x = 30;
}
public class Test {
public static void main(String[] args)
{
B b = new B();
System.out.println(b.x);
A a = new A();
System.out.println(a.x);
A a2 = new B();
System.out.println(a2.x);
}
}
Ans: Yes, code will be successfully compiled. The output is 30, 20, 20.
15. What is the output of executing code of class Test?
public class A {
void m1() {
System.out.println("m1 in class A");
}
}
public class B extends A {
void m1() {
System.out.println("m1 in class B");
}
}
public class Test {
public static void main(String[] args)
{
B b = new B();
b.m1();
A a = new A();
a.m1();
A a2 = new B();
a2.m1();
}
}
Ans: The result is m1 in class B, m1 in class A, m1 in class B.
16. Will this code run successfully? If yes, what will be the output?
public class A
{
private int x = 50;
void m1() {
System.out.println(x);
}
}
public class B extends A {
}
public class Test {
public static void main(String[] args)
{
A a = new B();
a.m1();
}
}
Ans: Yes, code will be successfully compiled. The output will be 50.
17. Will this code compile successfully? If yes, what is the output? If no,
identify the errors.
package pack1;
public class A
{
private int x = 50;
protected int y = 100;
int z = 200;
}
package pack2;
import pack1.A;
public class B extends A {
import pack2.B;
public class Test {
public static void main(String[] args)
{
B b = new B();
System.out.println(b.x);
System.out.println(b.y);
System.out.println(b.z);
}
}
Ans: No, the code will not compile successfully because of two compile-time errors.
First error is in line System.out.println(b.x); because private members cannot
be accessed in the subclass.
Second error is in line System.out.println(b.y); because default members of
superclass can be accessed in the subclass within the same package only.
18. A class member declared protected becomes which type of member in subclass?
Ans: A class member declared protected becomes private member of subclass.
19. What is Diamond ambiguity?
Ans: The Diamond Problem is an ambiguity that arises in multiple inheritance
when two parent classes inherit from the same grandparent class, and both parent
classes are inherited by a single child class