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.
- Priya deposited Rs. 'p' and Rs. (p + 400) in two schemes, A and B, for 2 years each. Schemes A and B offer 10% and 12% simple interest respectively. If the...
A certain sum when invested at compound interest of 8% p.a. amounts to Rs. 5832 in 2 years. Find the amount received when the same sum is invested at 12...
If Rs. 15000 is invested at 14% per annum, compounded yearly, calculate the interest earned in one year.
A man wants to invest Rs. 60660 in bank accounts of his two sons whose ages are 12 years and 16 years in such a way that they will get equal amount at a...
Ravi invests Rs. 'p' in Scheme 'X' and Rs. (p + 3,000) in Scheme 'Y'. Scheme X gives simple interest at 10% per annum for 24 months, while Scheme Y offe...
- Calculate the simple interest on Rs. 7,500 at the rate of 12% per annum for 18 months.
John borrowed some money from Alex at 10% simple interest per annum. He lended this money to Shaun at 20% compound interest per annum, and made a profit...
A sum doubles in 9 years at simple interest. Find the rate of interest per annum is.
A man invested certain sum at 9.5% p.a. simple interest for his son who was 14 years old. If the amount received by the son when he was 27 years old is ...
The simple interest earned on investing Rs. (80X + 2000) at simple interest of 15% p.a. for 36 months is Rs. 576 less than the total amount received on ...