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.
With reference to the Silk Production in India, which of the following statements is/are correct?
1. India is the world’s largest producer an...
_____was the first Mughal Emperor to acquire Koh-i-noor diamond
Which of the following statements best describes a journal in accounting?
The speed of a train is 90 km/hr and it takes 15 seconds to cross a man. Find the time taken by train to cross a platform if the ratio of the length of ...
An optical device Y has a positive focal length. Y is:
Weight of a body on the surface of the earth is W₁. The weight of the same body is W2, at a height of 500 metres above the surface, and W3 at a depth ...
Using 'lower of cost and net realisable value' for the purpose of inventory valuation is the implementation of which of the following concepts?
A shopkeeper sold an article after giving a discount of 20% and made a profit of Rs. 70. Find the marked price of the article if cost price of the artic...
Which of the following statements is/are correct?
I. Jainism rose in popularity due to its simple doctrines and the use of a language understood ...
Which of the following pairs is incorrect?