Reputation: 21
In Java inheritance, can the expression "overriding an abstract method" be used interchangeably with the expression "implementing an abstract method"?
I came across the following question in the book "OCA Java SE 8 Programmer I Study Guide" in Chapter 5, page 296, question #15:
Q: Which of the following is true about a concrete subclass? (Choose all that apply)
My answer was B & E. But the book says, the correct answer is only B. My question is specifically about the option E. The book says, option E is incorrect because abstract methods must be overridden by a concrete subclass. My initial thought was abstract methods must be implemented before being overridden by a subclass. So why is option be incorrect?
Upvotes: 2
Views: 7238
Reputation: 938
Let me explain each points one at a time.
A. A concrete subclass can be declared as abstract.
Ans: Incorrect.
By the definition, concrete classes are those which can be instantiated
and this class can also extends
the base abstract class.
B. A concrete subclass must implement all inherited abstract methods.
Ans: Correct
If the methods are marked as abstracts
in the base class, then the child class extending base class should make sure that all the abstract methods are implemented. You will otherwise get a compile time error
.
C. A concrete subclass must implement all methods defined in an inherited interface.
Ans: Incorrect
this is only the case when the concrete subclass has directly implemented the interface by this class itself
.
But in this case, base class may have implemented an inherited interface .Hence, its not mandatory for concrete sub-class to implement all methods that are there in base class as a result of inheritance from the interface.
D. A concrete subclass cannot be marked as final.
Ans: Incorrect
If a subclass is marked as final, then this class can no longer be extended by any other available sub-classes. Marking a class with a final
keyword is particularly used to achieve Immutability.
With Immutable classes, We can call accessor methods (e.g. getter methods), copy the objects, or pass the objects around — but no method should allow modifying the state of the object. Examples: String, wrapper classes like Float, Integer.etc.
E. Abstract methods cannot be overridden by a concrete subclass.
Ans: Incorrect
By the definition, Abstract methods are methods that are only declared but contains no body or implementation.And since we cannot instantiate abstract class, it is up-to sub classes to provide the implementations for these defined abstract methods.Hence abstract methods are not optional but mandatorily be overridden by a inheriting subclass
.
Upvotes: 2
Reputation: 2355
An abstract method is a method that is declared, but contains no implementation.
final
cannot be overridden.Upvotes: 1