Polymorphism Flashcards

(45 cards)

1
Q

What is Runtime Polymorphism?

A

Runtime polymorphism is when the method to execute is determined at runtime based on the actual object type.

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

What is another name for runtime polymorphism?

A

Dynamic polymorphism.

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

Dynamic polymorphism.

A

Method overriding.

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

When is method selection decided in runtime polymorphism?

A

At runtime, not at compile time.

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

What determines which overridden method is called?

A

The actual object type, not the reference type.

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

Example of runtime polymorphism?

A

Animal a = new Dog(); a.sound(); → Dog’s version runs.

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

Why is runtime polymorphism useful?

A

It enables flexible and extensible designs where behavior varies by object type.

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

Does runtime polymorphism require inheritance?

A

Yes. It requires a parent-child class relationship.

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

Does runtime polymorphism work with static methods?

A

No. Static methods use compile-time binding.

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

Is runtime polymorphism related to dynamic binding?

A

Yes. Runtime polymorphism is implemented through dynamic method dispatch.

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

What is dynamic method dispatch?

A

JVM decides at runtime which overridden method implementation to call.

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

Can private methods participate in runtime polymorphism?

A

No. Private methods cannot be overridden.

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

Can final methods participate in runtime polymorphism?

A

No. Final methods cannot be overridden.

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

What is the difference between compile-time and runtime polymorphism?

A

Compile-time uses overloading; runtime uses overriding.

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

Can runtime polymorphism be achieved using data members in Java?

A

No. Runtime polymorphism applies only to methods, not variables.

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

Key rule to remember?

A

Overriding + object type → runtime polymorphism.

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

Why can’t fields support runtime polymorphism?

A

Because variable access is resolved at compile time based on reference type, not object type.

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

What supports runtime polymorphism in Java?

A

Only overridden instance methods.

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

What happens when subclass defines a field with same name as parent?

A

It results in variable hiding, not polymorphism.

19
Q

What is variable hiding?

A

It occurs when a subclass declares a field with the same name as a superclass field.

20
Q

Which value is accessed for hidden variables?

A

The value is chosen based on reference type, not object type.

21
Q

Example of variable hiding behavior?

A

Parent p = new Child(); System.out.println(p.x); prints Parent’s x.

22
Q

Can fields be overridden?

A

No. Fields cannot be overridden; only methods can.

23
Q

Why does Java restrict polymorphism to methods?

A

Because methods can be dynamically dispatched, while fields are statically resolved.

24
Does method overriding affect field values?
No. Overriding only affects methods, not data members.
25
Can static variables participate in polymorphism?
No. Static variables are resolved at compile time.
26
Which concept enables runtime polymorphism?
Dynamic method dispatch.
27
Key rule to remember?
Methods override; variables hide.
28
What is the difference between static binding and dynamic binding in Java?
Static binding is resolved at compile time, while dynamic binding is resolved at runtime based on the actual object type.
29
What is static binding?
It is method or variable resolution determined during compilation.
30
What is dynamic binding?
It is method resolution determined at runtime using the object’s actual class.
31
Which methods use static binding?
Static, private, and final methods.
32
Which methods use dynamic binding?
Overridden instance methods.
33
Why is static binding faster?
Because the method call is resolved once at compile time.
34
Why is dynamic binding more flexible?
Because it allows behavior to change depending on the runtime object type.
35
Does method overloading use static or dynamic binding?
Static binding.
36
Does method overriding use static or dynamic binding?
Dynamic binding.
37
What determines dynamic binding execution?
The actual object type at runtime.
38
What determines static binding execution?
The reference type at compile time.
39
Example of dynamic binding?
Animal a = new Dog(); a.sound(); calls Dog’s method.
40
Example of static binding?
Math.max(1,2) is resolved at compile time.
41
Can static methods be dynamically bound?
No. Static methods use compile-time binding.
42
Can constructors use dynamic binding?
No. Constructors are statically bound.
43
Why is dynamic binding essential for polymorphism?
It allows different implementations to execute depending on object type.
44
Key rule to remember?
Compile-time resolution = static binding; runtime resolution = dynamic binding.