Inheritance is one of the core concepts of Object-Oriented Programming (OOP) and is foundational to Java. It allows developers to create a new class that is based on an existing class, inheriting its attributes and methods. If you’re keen to test your knowledge or learn more about inheritance in Java, this quiz is for you!
Each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. Which keyword is used to implement inheritance in Java?
Answer:
Explanation:
In Java, the extends keyword is used to declare inheritance, allowing a subclass to inherit properties and methods from a superclass.
2. What is a superclass (or parent class)?
Answer:
Explanation:
A superclass (or parent class) provides the basis from which subclasses can inherit properties and methods.
3. Which of the following concepts allows Java classes to inherit methods and properties from multiple classes?
Answer:
Explanation:
Multiple Inheritance allows a class to inherit properties and methods from multiple classes. However, Java doesn’t support multiple inheritance with classes but achieves it through interfaces.
4. Which keyword in Java is used to access the superclass’s members (methods/variables)?
Answer:
Explanation:
The super keyword in Java is used to refer to the immediate parent class instance variable or method.
5. Which method is called automatically when an object is created?
Answer:
Explanation:
A constructor is a special method that gets called automatically when an object is instantiated. It typically initializes the newly created object.
6. In Java, if a class does not have any constructor, what will Java do?
Answer:
Explanation:
If a class doesn’t have a constructor, Java automatically provides a default no-argument constructor.
7. What will happen if a subclass provides a constructor explicitly and does not invoke the parent class constructor?
Answer:
Explanation:
Even if the subclass provides its constructor and doesn’t explicitly invoke the superclass’s constructor, Java automatically calls the no-argument constructor of the superclass.
8. Which of these can be inherited from a superclass?
Answer:
Explanation:
Only public and protected members (methods and variables) of a superclass are inherited by a subclass. Private members are not inherited.
9. What is the purpose of inheritance in Java?
Answer:
Explanation:
The primary purpose of inheritance is to promote code reusability. By allowing one class to inherit properties and methods from another, developers can avoid redundant code.
10. Which of the following is false about inheritance in Java?
Answer:
Explanation:
While a subclass can override many superclass methods, it cannot override private superclass methods because private methods are not accessible in the subclass.
Inheritance plays a pivotal role in Java and OOP, driving efficient and organized code. Quizzes like these can bolster your understanding of core concepts. Whether you’re just beginning your Java journey or brushing up on the basics, continuous learning is key. Happy coding!