filmov
tv
25+ Tricky [Most Asked ] Core Java Interview Questions on Inheritance | OOPs concept Java | Part - 6

Показать описание
This session is fully dedicated to most asked interview questions on inheritance | OOPs concept JAVA
Q30 What is inheritance ?
Ans:-
It is the process of defining a new class from the existing class functionality. In order to inherit the existing class. We need to use the ‘extends’ keyword.
Inheritance is a process where a child class acquires all the properties and behaviors of the parent class
One of the most best examples of Inheritance in the real world is Father-Son relationship, where Son inherit Father's property.
Example :-
Class Father {
// super class
}
Class Child extends Father {
// sub class
}
Q31 Why do we need to use inheritance?
Ans:-
There are the following reasons to use inheritance in java.
Code Reuse:- We can reuse the code from the parent/base class.
Increase features:- Increase the features of class or method by overriding.
Features Resuse:- Inheritance is used to use the existing features of class.
Polymorphism :- Used to achieve runtime polymorphism i.e method overriding.
Q32 What are the types of inheritance in Java?
Ans:-
- Single Inheritance
It is also called as simple inheritance - a sub-class is inheritate from only one super class. It inherits the properties(member variables) and behavior (methods) of a single-parent class.
-Multi-level Inheritance
In multi-level inheritance, a class is inheritance from a class which is also inheritance from another class is called multi-level inheritance.
In simple words, we can say that a class that has more than one parent class is called multi-level inheritance.
Note that the classes must be at different levels. Hence, there exists a single base class and single derived class but multiple intermediate base classes.
- Hierarchical Inheritance
If a number of classes are inheritance from a single base class, it is called hierarchical inheritance.
- Hybrid Inheritance
Hybrid means consist of more than one. Hybrid inheritance is the combination of two or more types of inheritance.
Q33 Why multiple inheritance is not supported in java through class?
Ans:
First reason is ambiguity around the Diamond problem, consider a class A has foo() method and then B and C derived from A and has their own foo() implementation, and now class D derives from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke.
This is also called the Diamond problem because the structure on this inheritance scenario is similar to 4 edge diamond,
More convincing reason is that multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc so omitted it for the sake of simplicity.
Also, java avoids this ambiguity by supporting single inheritance with interfaces. Since the interface only has a method declaration and doesn't provide any implementation there will only be just one implementation of a specific method hence there would not be any ambiguity.
Q35 What is Is-A relationship in Java?
Ans:
Is-A relationship is called as Inheritance. It is implemented using the “extends” keyword. It is used for code reusability.
Q36 What is super class and subclass?
Ans:-
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, and nested classes) from other class is called subclass. It is also called a derived class, child class, or extended class.
Q37 How is Inheritance implemented/achieved in Java?
Ans:-
Using two keywords:
extends: extends is a keyword that is used for developing the inheritance between two classes and two interfaces.
implements: implements keyword is used for developing the inheritance between a class and interface.
Q38 Which class in Java is superclass of every other class?
Ans:-
Object class is the superclass of every other class.
Q39 Can a class extend itself??
Ans:-
No, a class cannot extend itself. Circular inheritance is not possible in Java
Q40 Can a class extend more than one class?
Ans:-
No , one class can extend only a single class.
Q41 Are constructor and instance initialization block inherited to subclass?
Ans:-
No, constructor and instance initialization block of the superclass cannot be inherited to its subclass but they are executed while creating an object of the subclass.
Q42 Are static members inherited to subclass in Java?
Ans:-
Static block cannot be inherited to its subclass.
A static method of superclass is inherited to the subclass as a static member and non-static method is inherited as a non-static member only.
Q43 Can we extend (inherit) final class?
Ans:-
No, a class declared with final keyword cannot be inherited.
Q44 Can a final method be overridden?
Ans:-
No, a final method cannot be overridden.
Q45 Can we inherit private members of base class to its subclass?
Ans:-
No
#java #corejava #javainterviewquestion
Q30 What is inheritance ?
Ans:-
It is the process of defining a new class from the existing class functionality. In order to inherit the existing class. We need to use the ‘extends’ keyword.
Inheritance is a process where a child class acquires all the properties and behaviors of the parent class
One of the most best examples of Inheritance in the real world is Father-Son relationship, where Son inherit Father's property.
Example :-
Class Father {
// super class
}
Class Child extends Father {
// sub class
}
Q31 Why do we need to use inheritance?
Ans:-
There are the following reasons to use inheritance in java.
Code Reuse:- We can reuse the code from the parent/base class.
Increase features:- Increase the features of class or method by overriding.
Features Resuse:- Inheritance is used to use the existing features of class.
Polymorphism :- Used to achieve runtime polymorphism i.e method overriding.
Q32 What are the types of inheritance in Java?
Ans:-
- Single Inheritance
It is also called as simple inheritance - a sub-class is inheritate from only one super class. It inherits the properties(member variables) and behavior (methods) of a single-parent class.
-Multi-level Inheritance
In multi-level inheritance, a class is inheritance from a class which is also inheritance from another class is called multi-level inheritance.
In simple words, we can say that a class that has more than one parent class is called multi-level inheritance.
Note that the classes must be at different levels. Hence, there exists a single base class and single derived class but multiple intermediate base classes.
- Hierarchical Inheritance
If a number of classes are inheritance from a single base class, it is called hierarchical inheritance.
- Hybrid Inheritance
Hybrid means consist of more than one. Hybrid inheritance is the combination of two or more types of inheritance.
Q33 Why multiple inheritance is not supported in java through class?
Ans:
First reason is ambiguity around the Diamond problem, consider a class A has foo() method and then B and C derived from A and has their own foo() implementation, and now class D derives from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke.
This is also called the Diamond problem because the structure on this inheritance scenario is similar to 4 edge diamond,
More convincing reason is that multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc so omitted it for the sake of simplicity.
Also, java avoids this ambiguity by supporting single inheritance with interfaces. Since the interface only has a method declaration and doesn't provide any implementation there will only be just one implementation of a specific method hence there would not be any ambiguity.
Q35 What is Is-A relationship in Java?
Ans:
Is-A relationship is called as Inheritance. It is implemented using the “extends” keyword. It is used for code reusability.
Q36 What is super class and subclass?
Ans:-
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, and nested classes) from other class is called subclass. It is also called a derived class, child class, or extended class.
Q37 How is Inheritance implemented/achieved in Java?
Ans:-
Using two keywords:
extends: extends is a keyword that is used for developing the inheritance between two classes and two interfaces.
implements: implements keyword is used for developing the inheritance between a class and interface.
Q38 Which class in Java is superclass of every other class?
Ans:-
Object class is the superclass of every other class.
Q39 Can a class extend itself??
Ans:-
No, a class cannot extend itself. Circular inheritance is not possible in Java
Q40 Can a class extend more than one class?
Ans:-
No , one class can extend only a single class.
Q41 Are constructor and instance initialization block inherited to subclass?
Ans:-
No, constructor and instance initialization block of the superclass cannot be inherited to its subclass but they are executed while creating an object of the subclass.
Q42 Are static members inherited to subclass in Java?
Ans:-
Static block cannot be inherited to its subclass.
A static method of superclass is inherited to the subclass as a static member and non-static method is inherited as a non-static member only.
Q43 Can we extend (inherit) final class?
Ans:-
No, a class declared with final keyword cannot be inherited.
Q44 Can a final method be overridden?
Ans:-
No, a final method cannot be overridden.
Q45 Can we inherit private members of base class to its subclass?
Ans:-
No
#java #corejava #javainterviewquestion