What is the output of this Java program demonstrating runtime polymorphism?
class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } public class Test { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } }
Remember that the method called depends on the actual object type, not the reference type.
The reference variable a is of type Animal, but it points to an object of type Dog. At runtime, the overridden sound() method in Dog is called, so the output is "Dog barks".
What will be printed when this Java code runs?
class Vehicle { void start() { System.out.println("Vehicle starts"); } } class Car extends Vehicle { @Override void start() { System.out.println("Car starts"); } } class SportsCar extends Car { @Override void start() { System.out.println("SportsCar starts quickly"); } } public class Test { public static void main(String[] args) { Vehicle v = new SportsCar(); v.start(); } }
Which class's start() method is called depends on the actual object type.
The reference v is of type Vehicle, but it points to a SportsCar object. The most specific overridden method start() in SportsCar is called at runtime, printing "SportsCar starts quickly".
What is the output of this Java program?
class Parent { void show() { System.out.println("Parent show"); } } class Child extends Parent { @Override void show() { System.out.println("Child show"); } void childOnly() { System.out.println("Child only method"); } } public class Test { public static void main(String[] args) { Parent p = new Child(); p.show(); ((Child) p).childOnly(); } }
Check which method is overridden and which method is only in the child class.
The show() method is overridden, so Child show is printed. The cast to Child allows calling childOnly(), so "Child only method" is printed next.
What will this Java program print?
class Base { String name = "Base"; void printName() { System.out.println(name); } } class Derived extends Base { String name = "Derived"; @Override void printName() { System.out.println(name); } } public class Test { public static void main(String[] args) { Base b = new Derived(); System.out.println(b.name); b.printName(); } }
Remember that variables are not polymorphic, but methods are.
The variable name accessed by b.name uses the reference type Base, so it prints "Base". The method printName() is overridden, so the Derived version runs and prints "Derived".
What is the output of this Java program using an abstract class and runtime polymorphism?
abstract class Shape { abstract void draw(); } class Circle extends Shape { @Override void draw() { System.out.println("Drawing Circle"); } } class Square extends Shape { @Override void draw() { System.out.println("Drawing Square"); } } public class Test { public static void main(String[] args) { Shape s = new Circle(); s.draw(); s = new Square(); s.draw(); } }
Abstract classes cannot be instantiated directly, but references can point to subclass objects.
The reference s is of type Shape. It first points to a Circle object, so draw() prints "Drawing Circle". Then it points to a Square object, so draw() prints "Drawing Square".