Inheritance is one of the key features of object oriented programming. Through inheritance, a class (Sub Class) can inherit properties of another class (Super Class). Sub class can have it’s own properties along with the inherited properties from it’s super class.
Single Inheritance :
One class is extended by only one class.
Multilevel Inheritance :
One class is extended by a class and that class in turn is extended by another class thus forming a chain of inheritance.
Hierarchical Inheritance :
One class is extended by many classes.
Hybrid Inheritance :
It is a combination of above types of inheritance.
Multiple Inheritance :
One class extends more than one classes. (Java does not support multiple inheritance.)
Co-variant return type states that return type of overriding method can be subtype of the return type declared in method of superclass. it has been introduced since jdk 1.5
The overriding methods can throw any runtime Exception , here in the case of runtime exception overriding method (subclass method) should not worry about exception being thrown by superclass method.
If superclass method does not throw any exception then while overriding, the subclass method can not throw any new checked exception but it can throw any runtime exception.
Different exceptions in java follow some hierarchy tree(inheritance). In this case, if superclass method throws any checked exception , then while overriding the method in subclass we can not throw any new checked exception or any checked exception which are higher in hierarchy than the exception thrown in superclass method.
No, a class in java can not extend more than one classes or java does not support multiple inheritance. To avoid ambiguity, complexity and confusion, java does not supports multiple inheritance.
For example, If Class C extends Class A and Class B which have a method with same name, then Class C will have two methods with same name. This causes ambiguity and confusion for which method to use. To avoid this, java does not supports multiple inheritance.
class A {
void methodOne(){
System.out.println("From method Of Class A");
}
}
class B {
void methodOne(){
System.out.println("From method Of Class B");
}
}
class C extends A,B (If it is supported) {
// two same methods will be inherited to Class C.
// This causes ambiguity and confusion.
}
No, A class can not extend itself.
Options :
class B + class A { }
class B inherits class A { }
class B extends A { }
class B extends class { }
Answer : c.
Explanation : None.
No, Constructors and initializers(Static initializers and instance initializers) are not inherited to sub classes. But, they are executed while instantiating a sub class.
Through interfaces, we can implement multiple inheritance in java. As classes in java can not extend more than one classes, but a class can implement more than one interfaces.
interface A {
}
interface B {
}
class C implements A,B {
// Class implementing two interfaces.
}
Super class field will be hidden in the sub class. You can access hidden super class field in sub class using super keyword.
Yes, Static members are also inherited to sub classes.
class A {
static int i = 10;
static void method() {
System.out.println("Static Method");
}
}
class B extends A {
}
public class StaticInitializers {
public static void main(String[] args) {
B.method(); // Calling inherited static method
System.out.println(B.i); // printing inherited static field.
}
}
Options :
public members of class can be accessed by any code in the program.
private members of class can only be accessed by other members of the class.
private members of class can be inherited by a sub class, and become protected members in sub class.
protected members of a class can be inherited by a sub class, and become private members of the sub class.
Answer : c.
Explanation : private members of a class cannot be inherited by a sub class.