Question
In Object-Oriented Programming, which of the following
Java code snippets correctly demonstrates inheritance ? class Animal {Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â void sound () { Â Â Â Â Â Â Â System.out.println( "Animal makes a sound" ); Â Â Â }} class Dog extends Animal { Â Â Â void sound () { Â Â Â Â Â Â Â System.out.println( "Dog barks" ); Â Â Â }}Solution
In this code, the Dog class inherits from the Animal class, and it overrides the sound() method. This is a clear example of inheritance and method overriding in object-oriented programming. Let's break down the code and explain why A is correct and the other options are wrong: ·        Explanation of Correct Option (A): The Dog class extends the Animal class, which means it inherits the sound() method from Animal . However, Dog provides its own version of sound() , printing "Dog barks" instead of "Animal makes a sound". This is method overriding : a subclass providing a specific implementation of a method that was already defined in its superclass. Method overriding allows the subclass to provide its own version of the inherited method. ·        Why the Other Options Are Incorrect:
- The Dog class cannot override the sound() method from the Animal class: This is incorrect because the Dog class is allowed to override the sound() method from Animal . Inheritance and overriding are core principles of object-oriented programming in Java.
- The Dog class is inheriting the sound() method from the Animal class but not overriding it: This is incorrect because the Dog class does indeed override the sound() method. It provides a new implementation for that method, which is characteristic of method overriding.
- The Animal class should be an abstract class to allow method overriding: This is incorrect. The Animal class does not need to be abstract for its methods to be overridden. Regular (non-abstract) methods can be overridden in Java.
- The Dog class should not inherit from Animal : This is incorrect because it is logical for a Dog to inherit from an Animal , as all dogs are animals. This reflects real-world relationships and follows the principles of inheritance in OOP.
Consider the following statements:
Assertion (A): Alcohol, instead of mercury, is used in thermometers to measure temperatures up to 60°C.
<...The question below consists of a statement followed by two assumptions numbered I and II. An assumption is something taken for granted or logically pre...
Consider the following statements:
Assertion (A): A heavy downpour helps reduce humidity in the atmosphere.
Reason (R): Rain occurs when t...
Statement: A construction worker was buried alive when mud from a 20-ft high pit collapsed on him and four others.
Assumption I: The other f...
Statement: Automobile majors began the new fiscal on a strong note, posting high double-digit growth in their domestic passenger vehicle sales.
<...A statement is given followed by two conclusions numbered I & II. Assuming the statement to be true, even if they seem to be at variance with commonly ...
Statement: As an ongoing mission to help taxpayers, the e-portal service has been set up by the Additional Commissioner of Income Tax at the taxpayers...
Statement: Farm fires, being touted as the major reason for unbreathable Delhi air, are shown to account for only 40% of the current spell of deadly h...
Statement:
The government has launched a nationwide nutrition programme to address rising cases of childhood malnutrition. It argues that ensuri...Statement : "The company has launched a new app to address customer grievances quickly."
Assumptions :
I. Customers may find the app eas...