Which of the following is true about inheritance in Java?

Java MCQ: Which of the following is true about inheritance in Java?

a) Inheritance allows a class to inherit fields and methods from another class
b) Java supports multiple inheritance through classes
c) Inheritance is implemented using the ‘implements’ keyword
d) Inheritance prevents method overriding

Answer:

a) Inheritance allows a class to inherit fields and methods from another class

Explanation:

Inheritance in Java is a mechanism where one class (the subclass) inherits fields and methods from another class (the superclass). This promotes code reuse and establishes a natural hierarchy between classes. The subclass can also override methods from the superclass to provide specific implementations.

Java does not support multiple inheritance through classes, meaning a class cannot inherit from more than one class. However, Java does support multiple inheritance through interfaces, allowing a class to implement multiple interfaces.

The extends keyword is used to inherit from a class, while the implements keyword is used to implement interfaces. Understanding inheritance is key to building flexible and maintainable object-oriented systems in Java.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top