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.
The fifth volume of the Reserve Bank of India’s history is released, this volume encompasses the period from ______.
How has the sovereign gold bonds (SGBs) scheme impacted India's gold import bill in the financial year 2023-24?
Canada Pension Plan Investment Board (CPPIB) has sold 1.7 per cent stake in private sector lender Kotak Mahindra Bank, mopping up to _______ .
The IIT Hyderabad has created India's first AI-triggered accessible job portal for persons with disabilities (PwDs). Name it?
What was China's share in global EV exports in 2024?
Recently, Japan successfully conducted a test-fire of what advanced technology from an offshore platform?
What share did UPI account for in total payment volume as per the RBI report?
To prevent cancer ‘Hope Express’ has been announced by the Health Minister of state ______ Rajesh Tope?
HyderabadDLabs at the Indian School of Business (ISB) has launched ‘Build for Billions in association with Reserve Bank Innovation Hub (RBIH) a st...
________ signed a professional services agreement with Cloud Software Group, to help implement, upgrade, modernize and provide services for its TIBCO pr...