Implement inheritance including visibility modifiers and composition.
Implement polymorphism.
Develop code that uses abstract classes and methods.
1. The correct answer is B.
The method:
protected boolean equals(Question_1_3 q)
Doesn't override the equals method from java.lang.Object
(look at the access modifier). For that reason, the if
condition fails. Note that if the method call were:
if(q1.equals(q2)) { ...
We will be calling the method with the protected
modifier, making the condition true.
2. The correct answer is D.
The proper signatures of the methods are:
public int hashCode()
public boolean equals(Object obj)
public String toString()
So the only correct answer is option D.
3. The correct answer is A.
You could say that the overload is ambiguous, and compilation fails, since null
can be assigned to any type, but in fact, there's a rule that says the in overloading the most SPECIFIC one is chosen. Since Integer
is more specific than Object
, the former is selected.
4. The correct answer is B.
Static methods are hidden not overridden. Since the main method is in the subclass, the subclass method is called.
The call to Question_2_4.print()
yields the same result:
Subclass
The call to SuperClass.print()
yields:
Superclass
5. The correct answer is A.
Static methods are allowed in abstract
classes, the only restriction being that they cannot be abstract
. All methods that are accessible are inherited by subclasses. If an inherited method is static
, the only difference is that the method can be hidden instead of overridden.
6. The correct answer is B.
Even when the variable is cast to SuperClass3
, Java chooses to execute the method of the subclass thanks to polymorphism. If we want to execute the method of the superclass, we have to instantiate an object from that class.