This set of Java Interview Questions and Answers for freshers focuses on “Inheritance – 2”.
1. What is not type of inheritance?
a) Single inheritance
b) Double inheritance
c) Hierarchical inheritance
d) Multiple inheritance
Explanation: Inheritance is way of acquiring attributes and methods of parent class. Java supports hierarchical inheritance directly.
2. Using which of the following, multiple inheritance in Java can be implemented?
c) Protected methods
d) Private methods
Explanation: Multiple inheritance in java is implemented using interfaces. Multiple interfaces can be implemented by a class.
3. All classes in Java are inherited from which class?
Explanation: All classes in java are inherited from Object class. Interfaces are not inherited from Object Class.
4. In order to restrict a variable of a class from inheriting to subclass, how variable should be declared?
Explanation: By declaring variable private, the variable will not be available in inherited to subclass.
5. If super class and subclass have same variable name, which keyword should be used to use super class?
Explanation: Super keyword is used to access hidden super class variable in subclass.
6. Static members are not inherited to subclass.
Explanation: Static members are also inherited to subclasses.
7. Which of the following is used for implementing inheritance through an interface?
Explanation: Interface is implemented using implements keyword. A concrete class must implement all the methods of an interface, else it must be declared abstract.
8. Which of the following is used for implementing inheritance through class?
Explanation: Class can be extended using extends keyword. One class can extend only one class. A final class cannot be extended.
9. What would be the result if a class extends two interfaces and both have a method with same name and signature?
a) Runtime error
b) Compile time error
c) Code runs successfully
d) First called method is executed successfully
Explanation: In case of such conflict, compiler will not be able to link a method call due to ambiguity. It will throw compile time error.
10. Does Java support multiple level inheritance?
Explanation: Java supports multiple level inheritance through implementing multiple interfaces.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java for Interviews, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.