Overloading Methods Flashcards

1
Q

Rules to overload methods…

A

Two methods will be treated as overloaded if both follow the mandatory rules below:

Both must have the same method name.
Both must have different argument lists. (order of the arguments matters)

And if both methods follow the above mandatory rules, then they may or may not:

Have different return types.
Have different access modifiers.
Throw different checked or unchecked exceptions.

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

Rules to override methods…

A

With respect to the method it overrides, the overriding method must follow following mandatory rules:

It must have the same method name.
It must have the same arguments.

It must have the same return type. From Java 5 onward, the return type can also be a subclass (subclasses are a covariant type to their parents).

It must not have a more restrictive access modifier (if parent –> protected then child –> private is not allowed).
It must not throw new or broader checked exceptions.

And if both overriding methods follow the above mandatory rules, then they:

May have a less restrictive access modifier (if parent –> protected then child –> public is allowed).

May throw fewer or narrower checked exceptions or any unchecked exception.

Apart from the above rules, there are also some facts to keep in mind:

Only inherited methods can be overridden. That means methods can be overridden only in child classes.

Constructors and private methods are not inherited, so they cannot be overridden.

Abstract methods must be overridden by the first concrete (non-abstract) subclass.

final methods cannot be overridden.

A subclass can use super.overridden_method() to call the superclass version of an overridden method.

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

Will it output true or false?

System.out.println(“str1.equals(str1=str2) = “ + str1.equals(str1 = str2));

A

False.
System.out.println(“str1.equals(str1=str2) = “ + str1.equals(str1 = str2));
First the equals method is evaluated and then the assignment is made (str1 = str2).
However if the method is called again on those two strings will return true since the str1 is already pointing to str2 in the poll, and both have the same hashcode.

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

Will it output true or false?
String str1 = “Hello”;
String str2 = “Hello”;
System.out.println(“str1 == str2 = “ + str1 == str2);

A

False.
String str1 = “Hello”;
String str2 = “Hello”;
System.out.println(“str1 == str2 = “ + str1 == str2);

The first parameter of sout is a string “str1 == str2 = “ , that in turn will concatenate with str1, and only after the full string is compared with str2.

-> "str1 == str2 = " + str1 => "str1 == str2 = Hello" == "Hello"
=> = false
To make it work as intended the comparison must be wrapped parenteses:
System.out.println("str1 == str2 = " + (str1 == str2));
 // output true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly