1/7
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is polymorphism?
Enables a single interface (method or type) to represent multiple types. This is achieved through Method Overloading, Method Overriding, Upcasting/Downcasting.
What are the benefits of polymorphism?
Reusability, flexibility and maintainability.
What is method overloading?
Compile-time polymorphism, where o you use the same method name with different parameters for different constructors (in the same class). Avoid overloading with nearly identical parameter lists.
What is Method overriding?
Run-time polymorphism, where you redefine a superclass method in a subclass, determined at run time through the @Override keyword. This is basically just inheritance!
What is upcasting?
Assigns a subclass object to a superclass reference.
Animal a = new Dog(); //Upcasting
a.makeSound(); //Calls the dog’s version at runtime.
What is downcasting?
Converts superclass reference back to subclass.
Animal a = new Dog();
If (a instanceof Dog) { Dog d = (Dog) a; } //Downcasting
What aspect of abstraction is also polymorphism?
Interfaces, because you are using the same method signature, but promotes loose coupling where components interact via interface, not class.