List out 2 types of Polymorphism
What is this type of Polymorhpism?
public class Calculator {
// Overloaded method 1: takes two integers
public int add(int a, int b) {
return a + b;
}
// Overloaded method 2: takes three integers
public int add(int a, int b, int c) {
return a + b + c;
}
}Compile-Time Polymorphism (Method Overloading)
What is this type of Polymorphism?
Ball b1 = new BaseBall(); // b1 holds a BaseBall object Ball b2 = new BasketBall(); // b2 holds a BasketBall object b1.hit(10); // Executes the BaseBall.hit() method b2.hit(10); // Executes the BasketBall.hit() method
Runtime Polymorphism (Method Overriding)
Can you tell the output at MainTest?
public class A {
public static void printType() { // Line 1
System.out.println("Type A: Static");
}
public void execute() { // Line 2
System.out.println("Type A: Instance");
}
}
public class B extends A {
public static void printType() { // Line 3 (Method Hiding)
System.out.println("Type B: Static");
}
public void execute() { // Line 4 (Method Overriding)
System.out.println("Type B: Instance");
}
}
public class MainTest {
public static void main(String[] args) {
A obj = new B(); // Line 5: Superclass reference, Subclass object
obj.printType(); // Statement X
obj.execute(); // Statement Y
}
}Polymorphism does not apply to static methods
public class Processor {
// Overloaded method 1
public void process(double d) {
System.out.println("Processing double: " + d);
}
// Overloaded method 2
public void process(long l) {
System.out.println("Processing long: " + l);
}
}
public class MainTest {
public static void main(String[] args) {
Processor p = new Processor();
int value = 5;
p.process(value); // Statement Z
}
}What is the output?
Processing long : 5
Can the code below compile successfully?
class Document {}
class PDF extends Document {}
public class Printer {
public Document print(String data) { // Line 7
return new Document();
}
}
public class LaserPrinter extends Printer {
@Override
public PDF print(String data) { // Line 8
return new PDF();
}
}Yes, since PDF extends Document, the return type PDF is valid covariant return type for Document
If class Printer return new PDF and class LaserPrinter return new Document, it will have runtime error
When a superclass reference holds a subclass object, what determines which methods are visible to the compiler?
The Reference Type (the superclass type) determines visibility. If the method isn’t in the superclass, the compiler can’t see it, even if the object has it.
Given Animal a = new Dog();, what keyword is used to confirm the object’s actual type at runtime?
The instanceof keyword. Example: if (a instanceof Dog) is used before downcasting to prevent a ClassCastException.
What is Downcasting and why is it sometimes necessary in a polymorphic system?
Downcasting converts a superclass reference to a subclass reference (Dog d = (Dog) a;). It is necessary when you need to access methods or fields that exist only in the subclass.
What is Upcasting and why is it safe?
Upcasting converts a subclass reference to a superclass reference (Animal a = new Dog();). It is safe because a subclass object is always an instance of its superclass.
Can methods that are package-private be overridden by a subclass in a different package?
No, they cannot. Package-private methods are not visible outside their package. Since they cannot be seen, they cannot be overridden
Can constructors participate in Method Overloading or Method Overriding?
Constructors can be overloaded (different parameter lists). Constructors cannot be overridden
If a method has the same name as an overloaded method but different parameters, and also throws a different exception, is this legal?
Yes, it is legal