Method Overloading and Overriding Flashcards

(182 cards)

1
Q

What is another name for method overloading?

A

Static polymorphism.

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

Why is method overloading called static polymorphism?

A

Because method resolution happens at compile time based on parameter types.

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

What is method overloading in Java?

A

It is defining multiple methods with the same name but different parameter lists in the same class.

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

What must differ for methods to be overloaded?

A

Parameter number, types, or order must differ.

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

Can return type alone differentiate overloaded methods?

A

No. Return type alone is not sufficient for overloading.

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

When is overloaded method selection decided?

A

At compile time.

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

What is compile-time polymorphism?

A

Polymorphism where method binding is determined during compilation.

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

Is method overloading runtime polymorphism?

A

No. Overloading is compile-time polymorphism; overriding is runtime polymorphism.

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

Example of method overloading?

A

add(int a,int b) and add(double a,double b).

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

Can constructors be overloaded?

A

Yes. Constructor overloading is common.

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

Does inheritance matter for method overloading?

A

No. Overloading can occur within the same class without inheritance.

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

Why is method overloading useful?

A

It improves readability and allows same method name for related operations.

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

What is the key advantage of static polymorphism?

A

Faster execution because method binding occurs at compile time.

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

What happens if overloaded methods are ambiguous?

A

The compiler throws an ambiguity error.

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

How do you implement method overloading in Java?

A

By defining multiple methods with the same name in the same class but different parameter lists.

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

What must change to overload a method?

A

Parameter number, parameter types, or parameter order must differ.

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

Can methods be overloaded with different number of parameters?

A

Yes. Example: add(int a,int b) and add(int a,int b,int c).

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

Can methods be overloaded with different parameter types?

A

Yes. Example: add(int a,int b) and add(double a,double b).

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

Can parameter order be used for overloading?

A

Yes. Example: method(int,String) and method(String,int).

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

Is return type enough to overload methods?

A

No. Methods must differ in parameters, not just return type.

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

Does method name change in overloading?

A

No. Method name must remain the same.

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

Is method overloading compile-time or runtime?

A

Compile-time polymorphism.

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

Can static methods be overloaded?

A

Yes. Static methods can also be overloaded.

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

Can constructors be overloaded?

A

Yes. Constructor overloading is common for flexible object creation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Does inheritance affect method overloading?
No. Overloading can occur in the same class without inheritance.
26
What happens if overloaded methods match equally well?
The compiler throws an ambiguous method call error.
27
Why is method overloading useful?
It improves readability and allows logically related operations under one method name.
28
Real example of method overloading in Java API?
System.out.println() which accepts different parameter types.
29
What is the compiler’s role in overloading?
The compiler determines which method to call based on argument types.
30
What argument variations are allowed in method overloading?
Methods can differ by number of parameters, parameter types, or parameter order.
31
Can method overloading differ by number of parameters?
Yes. Different parameter counts create valid overloads.
32
Can method overloading differ by parameter types?
Yes. Same number but different data types is valid.
33
Can method overloading differ by parameter order?
Yes. Same types but different order is valid if types differ.
34
Is changing only return type valid for overloading?
No. Return type alone cannot differentiate overloaded methods.
35
Can access modifiers differ in overloaded methods?
Yes. Access modifiers can differ but do not define overloading.
36
Can method names differ in overloading?
No. Method names must be the same for overloading.
37
Does method overloading require inheritance?
No. It can occur within the same class.
38
Does Java consider parameter names for overloading?
No. Only parameter types and order matter.
39
Is varargs considered different from array parameters?
Yes. varargs can participate in overloading but may cause ambiguity.
40
What happens if overload resolution is ambiguous?
The compiler throws a compile-time ambiguity error.
41
Does autoboxing affect overload resolution?
Yes. Java chooses the most specific method based on conversion rules.
42
Does type promotion affect overload resolution?
Yes. Widening conversions may be chosen if no exact match exists.
43
Which is preferred in overload resolution: exact match or promotion?
Exact match is preferred over type promotion.
44
Example of valid overload set?
method(int), method(double), method(int,int).
45
Why can’t method overloading be done by changing only return type in Java?
Because method calls are resolved based on method signature (name + parameters), not return type, so changing only return type causes ambiguity.
46
What defines a method signature in Java?
Method name and parameter list; return type is not part of the signature.
47
Why does return type not help overload resolution?
Because the compiler determines which method to call before knowing how the return value will be used.
48
What compile-time error occurs if only return type differs?
A duplicate method error because the signatures are considered identical.
49
Example of invalid overloading by return type?
int sum(int a,int b) and double sum(int a,int b) → compile error.
50
Can return type differ if parameters differ?
Yes. Different parameters make methods distinct, so return type can also differ.
51
How does the compiler choose overloaded methods?
It selects the method whose parameters best match the arguments.
52
Would this be valid: int f() and double f(int x)?
Yes, because parameters differ.
53
Would this be valid: int f() and double f()?
No, because signatures are identical.
54
Is return type used at runtime for method selection?
No. Method resolution happens at compile time.
55
Why did Java designers forbid return-type-only overloading?
To prevent ambiguity and ensure predictable method resolution.
56
How do you achieve different return types properly?
Use different method names or different parameter lists.
57
Does method overriding allow different return types?
Yes, but only covariant return types in subclasses.
58
What is a covariant return type?
A subclass method can return a subtype of the parent method’s return type.
59
Key rule to remember about overloading?
Parameters must differ; return type alone is insufficient.
60
Is it allowed to overload the main() method in Java?
Yes. You can overload main() with different parameter lists, but JVM only executes the standard entry-point version.
61
Which main method does JVM execute?
Only public static void main(String[] args).
62
Can you define multiple main methods in one class?
Yes. As long as their parameter lists differ, they are valid overloads.
63
Will JVM run overloaded main methods automatically?
No. JVM only invokes the exact entry-point signature.
64
Can overloaded main methods be called manually?
Yes. They can be called from inside code like any other method.
65
Example of overloaded main method?
public static void main(int x) {}
66
Does overloading main affect program execution?
No. It does not affect startup because JVM ignores non-standard signatures.
67
Can main be overloaded with different parameter types?
Yes. For example main(String), main(int[]), main(String,int).
68
Why might developers overload main?
For testing or demonstration purposes.
69
Can overloaded main methods be non-static?
Yes, but they won’t be entry points.
70
Does main overloading follow same rules as other methods?
Yes. It must differ in parameter list.
71
Will the program compile if only overloaded main methods exist?
Yes, but it won’t run because JVM won’t find the required signature.
72
What error occurs if standard main is missing?
Runtime error: main method not found.
73
Is main overloading commonly used in production code?
No. It is rarely used outside demonstrations or testing.
74
Key rule for main execution?
JVM always looks specifically for public static void main(String[] args).
75
How do you implement method overriding in Java?
By defining a method in a subclass with the same name, parameters, and return type as a method in the superclass.
76
What are the requirements for method overriding?
Same method name, same parameter list, compatible return type, and subclass relationship.
77
What is runtime polymorphism?
It is when a superclass reference calls a subclass’s overridden method and the method executed is determined at runtime.
78
What keyword helps indicate overriding?
@Override annotation.
79
Why use @Override annotation?
It ensures compile-time checking and prevents accidental method signature mistakes.
80
Can private methods be overridden?
No. Private methods are not inherited, so they cannot be overridden.
81
Can static methods be overridden?
No. Static methods can only be hidden, not overridden.
82
Can final methods be overridden?
No. final prevents method overriding.
83
Can return type differ in overriding?
Yes, but only covariant return types (subtype of original return type).
84
Can access modifier change in overriding?
Yes, but it cannot be more restrictive than the superclass method.
85
Example of overriding rule violation?
Changing parameter list results in overloading, not overriding.
86
What happens if superclass reference points to subclass object?
The subclass’s overridden method executes at runtime.
87
Why is method overriding important?
It enables runtime polymorphism and dynamic behavior.
88
Difference between overloading and overriding?
Overloading happens in same class at compile time; overriding happens across classes at runtime.
89
When is superclass method still callable after override?
Using super.methodName() inside subclass method.
90
Can static methods be overridden in Java?
No. Static methods cannot be overridden because they belong to the class, not instances.
91
What happens if a subclass defines a static method with the same signature as its superclass?
It is method hiding, not overriding.
92
What is method hiding?
Method hiding occurs when a subclass defines a static method with the same signature as a static method in the superclass.
93
Why can static methods not be overridden?
Because overriding relies on runtime polymorphism, but static methods are resolved at compile time.
94
Is static method binding compile-time or runtime?
Compile-time.
95
Can static methods participate in polymorphism?
No. They do not support runtime polymorphism.
96
Difference between overriding and hiding?
Overriding applies to instance methods and runtime binding; hiding applies to static methods and compile-time binding.
97
Which method executes when static methods are hidden?
The method is chosen based on reference type, not object type.
98
Example scenario of static method hiding?
If Parent ref = new Child(); ref.staticMethod() calls Parent’s version.
99
Can static methods be declared final?
Yes, but it has little practical effect since they cannot be overridden anyway.
100
Can static methods be abstract?
No. Abstract methods must be overridden, which static methods cannot be.
101
Can instance methods override static methods?
No. Mixing static and instance signatures causes compile-time errors.
102
Can static methods override instance methods?
No. Java forbids changing static/instance nature during override.
103
Key rule about static methods?
They are class-level methods resolved using reference type.
104
Why does Java not allow overriding static methods?
Because overriding depends on runtime polymorphism using object instances, but static methods belong to the class and are resolved at compile time.
105
Why is overriding tied to object instances?
Because overridden methods are selected based on the actual object type at runtime.
106
Why don’t static methods participate in polymorphism?
Because static methods are bound at compile time using the reference type, not object type.
107
What would happen if static methods could be overridden?
It would break Java’s method resolution model and create ambiguity in class-level method calls.
108
What is the real mechanism when subclass defines same static method?
It is method hiding, not overriding.
109
What is method hiding?
When a subclass defines a static method with the same signature as its superclass.
110
How does method hiding resolve calls?
The method is chosen based on reference type, not actual object type.
111
Example of static method hiding behavior?
Parent p = new Child(); p.show(); calls Parent.show() if show is static.
112
Can static methods support runtime dispatch?
No. Only instance methods support dynamic method dispatch.
113
Which binding does overriding require?
Runtime (dynamic) binding.
114
Which binding do static methods use?
Compile-time (static) binding.
115
Why is Java’s design decision beneficial?
It keeps method resolution predictable, simple, and efficient.
116
Key difference between static and instance methods in inheritance?
Instance methods override; static methods hide.
117
Does JVM look at object type for static methods?
No. JVM looks at reference type.
118
Core rule to remember?
Overriding requires instance context; static methods have none.
119
Is it allowed to override an overloaded method in Java?
Yes. A method that is overloaded in a superclass can be overridden in a subclass if the subclass overrides one of its versions.
120
What does it mean to override an overloaded method?
It means the subclass provides its own implementation for one specific overloaded version inherited from the parent class.
121
Can all overloaded versions be overridden?
Yes. A subclass can override none, one, or all overloaded variants individually.
122
Does overriding affect all overloaded methods automatically?
No. Each overloaded method is treated as a separate method and must be overridden individually.
123
Example scenario of overriding overloaded methods?
If parent has show(int) and show(String), subclass may override only show(int).
124
What determines which overloaded method is called?
Compile-time argument matching.
125
What determines which overridden method is executed?
Runtime object type.
126
Can a subclass both overload and override methods?
Yes. It can override inherited methods and also add new overloaded versions.
127
Does method signature have to match for overriding?
Yes. Name, parameters, and compatible return type must match.
128
If parameters differ, is it overriding or overloading?
Overloading, not overriding.
129
Can static overloaded methods be overridden?
No. Static methods cannot be overridden, only hidden.
130
Can private overloaded methods be overridden?
No. Private methods are not inherited.
131
Why is it important to distinguish overloading vs overriding?
Because they differ in binding time, behavior, and polymorphism support.
132
What annotation helps verify overriding correctness?
@Override.
133
Key rule to remember?
Overloading is compile-time; overriding is runtime.
134
What is the difference between method overloading and method overriding in Java?
Overloading is compile-time polymorphism within the same class using different parameters, while overriding is runtime polymorphism across parent-child classes using the same method signature.
135
Where does method overloading occur?
Within the same class.
136
Where does method overriding occur?
Between superclass and subclass.
137
What must differ in method overloading?
Parameter list must differ in number, type, or order.
138
What must match in method overriding?
Method name, parameters, and compatible return type must match.
139
Is overloading compile-time or runtime?
Compile-time.
140
Is overriding compile-time or runtime?
Runtime.
141
Does overloading require inheritance?
No.
142
Does overriding require inheritance?
Yes.
143
Which supports runtime polymorphism?
Method overriding.
144
Which supports compile-time polymorphism?
Method overloading.
145
Can overloaded methods have different return types?
Yes, if parameters differ.
146
Can overridden methods change return type?
Yes, but only covariant return types.
147
Can static methods be overloaded?
Yes.
148
Can static methods be overridden?
No. They can only be hidden.
149
Which annotation is used for overriding?
@Override.
150
What happens if parameters differ in subclass method?
It becomes overloading, not overriding.
151
Which is faster: overloading or overriding?
Overloading is faster because it is resolved at compile time.
152
Key memory trick?
Overloading = same class + different params; Overriding = different class + same params.
153
Does Java support virtual functions?
Yes. By default, all non-static, non-final, non-private instance methods in Java are virtual.
154
What is a virtual function?
A method whose implementation is determined at runtime based on the actual object type.
155
Are all methods virtual in Java?
No. Static, final, and private methods are not virtual.
156
Why are instance methods virtual by default in Java?
To support runtime polymorphism and dynamic method dispatch.
157
What is dynamic method dispatch?
It is the process where JVM decides which method implementation to execute at runtime.
158
Can static methods be virtual?
No. Static methods use compile-time binding.
159
Can private methods be virtual?
No. Private methods cannot be overridden.
160
Can final methods be virtual?
No. final prevents overriding.
161
Which methods support overriding?
Only non-static, non-final, non-private instance methods.
162
Why are constructors not virtual?
Because constructors are not inherited and cannot be overridden.
163
What determines which virtual method runs?
The actual object type, not the reference type.
164
Example of virtual method behavior?
Animal a = new Dog(); a.sound(); calls Dog’s version if overridden.
165
Is Java keyword virtual required like C++?
No. Java methods are virtual by default without needing a keyword.
166
Why did Java designers make methods virtual by default?
To simplify polymorphism and reduce boilerplate syntax.
167
Key rule to remember?
Instance methods → virtual by default; static/private/final → non-virtual.
168
What is a covariant return type in Java?
A covariant return type allows an overriding method to return a subtype of the return type declared in the superclass method.
169
What does "covariant" mean in return types?
It means the return type can be more specific (narrower) in the subclass.
170
Example of covariant return type?
If superclass method returns Animal, subclass can override it and return Dog.
171
Was covariant return type always allowed in Java?
No. It was introduced in Java 5; before that, return types had to match exactly.
172
Why are covariant return types useful?
They provide more specific return values and reduce the need for casting.
173
What is the rule for overriding return types?
The overridden method can return the same type or a subclass of that type.
174
Can primitive return types be covariant?
No. Covariant return types apply only to object reference types.
175
Is this valid overriding?
Parent: Object get(); Child: String get(); → Yes.
176
Is this valid overriding?
Parent: String get(); Child: Object get(); → No.
177
Does covariant return type affect polymorphism?
No. Polymorphism still works normally; only return specificity changes.
178
Why is covariant return type safer?
Because it preserves type compatibility while allowing more precise return values.
179
Does overloading use covariant return types?
No. Covariant return types apply only to method overriding.
180
What compile error occurs if return type is incompatible?
Compiler reports method does not override or incompatible return type.
181
Do interfaces support covariant return types?
Yes. Implementing classes can return subtype results.
182
Key rule to remember?
Overriding return type can be same or narrower, never broader.