📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!

  • google app store apple app store
  • ✖

      Question

      What does the following Java code snippet

      demonstrate? class Animal {     void sound() { System.out.println("Some sound"); } } class Dog extends Animal {     void sound() { System.out.println("Bark"); } }
      A Abstraction Correct Answer Incorrect Answer
      B Encapsulation Correct Answer Incorrect Answer
      C Method Overloading Correct Answer Incorrect Answer
      D Method Overriding Correct Answer Incorrect Answer
      E Constructor Chaining Correct Answer Incorrect Answer

      Solution

      Method overriding occurs when a subclass (Dog) provides a specific implementation of a method already defined in its superclass (Animal) with the same name, return type, and parameters. The JVM decides at runtime which version to call based on the actual object type. Tthis is runtime polymorphism. Method overloading (compile-time polymorphism) uses the same method name with different parameters within the same class.

      Practice Next

      Relevant for Exams:

      ask-question