Polymorphism in Java
Polymorphism in Java - Important Points
26. | Which of the following is an example of upcasting in Java? |
---|
A. Dog d = new Dog(); Animal a = d;
B. Animal a = new Animal(); Dog d = (Dog) a;
C. Dog d = new Animal();
D. None of the above
View Answer Discuss Work SpaceAnswer: option a
Explanation:
Upcasting is the process of converting an object of a subclass to an object of its superclass. In this example, the Dog object d is upcast to an Animal object a.
27. | Which of the following is an example of downcasting in Java? |
---|
A. Dog d = new Dog(); Animal a = d; Dog d2 = (Dog) a;
B. Animal a = new Animal(); Dog d = (Dog) a;
C. Dog d = new Animal(); Animal a = d;
D. None of the above
View Answer Discuss Work SpaceAnswer: option b
Explanation:
Downcasting is the process of converting an object of a superclass to an object of its subclass. In this example, the Animal object a is downcast to a Dog object d.
28. | Which of the following is an example of run-time polymorphism? |
---|
A. Method overloading
B. Method overriding
C. Operator overloading
D. None of the above
View Answer Discuss Work SpaceAnswer: option b
Explanation:
Run-time polymorphism is achieved through method overriding, where the implementation of a method is determined at runtime based on the actual type of the object that the method is called on.