Polymorphism Flashcards

(13 cards)

1
Q

List out 2 types of Polymorphism

A
  1. Compile-Time Polymorphism (Method Overloading)
  2. Runtime Polymorphism (Mothod Overriding)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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;
    }
}
A

Compile-Time Polymorphism (Method Overloading)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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
A

Runtime Polymorphism (Method Overriding)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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
    }
}
A
  1. Type A: Static
  2. Type B: Instance

Polymorphism does not apply to static methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
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?

A

Processing long : 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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();
    }
}
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When a superclass reference holds a subclass object, what determines which methods are visible to the compiler?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given Animal a = new Dog();, what keyword is used to confirm the object’s actual type at runtime?

A

The instanceof keyword. Example: if (a instanceof Dog) is used before downcasting to prevent a ClassCastException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Downcasting and why is it sometimes necessary in a polymorphic system?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Upcasting and why is it safe?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can methods that are package-private be overridden by a subclass in a different package?

A

No, they cannot. Package-private methods are not visible outside their package. Since they cannot be seen, they cannot be overridden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can constructors participate in Method Overloading or Method Overriding?

A

Constructors can be overloaded (different parameter lists). Constructors cannot be overridden

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

If a method has the same name as an overloaded method but different parameters, and also throws a different exception, is this legal?

A

Yes, it is legal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly