2

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)

  • A. A concrete subclass can be declared as abstract.
  • B. A concrete subclass must implement all inherited abstract methods.
  • C. A concrete subclass must implement all methods defined in an inherited interface.
  • D. A concrete subclass cannot be marked as final.
  • E. Abstract methods cannot be overridden by a concrete subclass.

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?

Phil
  • 157,677
  • 23
  • 242
  • 245
rusmusus
  • 21
  • 1
  • 5
  • It's just a poorly worded question. You have the right idea. – 4castle Feb 02 '18 at 02:59
  • 1
    You're thinking of overriding in terms of changing implementation. If that were the case, as in that's the target definition, there would be no such thing as "*overriding an abstract method*". Did you think it was a trick question? – Vince Feb 02 '18 at 03:32
  • I imagine OP is confused by the `@Override` annotation. This is just a handy pointer that the original definition **and / or** implementation of the method signature may be found in a super class. – Phil Feb 02 '18 at 04:17

2 Answers2

3

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.

Community
  • 1
  • 1
ishwor kafley
  • 938
  • 14
  • 32
1

An abstract method is a method that is declared, but contains no implementation.

  • you can override both abstract and normal methods inside an abstract class.
  • only methods declared as final cannot be overridden.
Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29
  • Your last sentence doesn't sound right; you can override existing implementation unless it's marked `final` – Phil Feb 02 '18 at 04:00
  • I am regarding this question.why the book says that E is incorrect. please read his question.IT's clear that you cant override a final method.I am talking about abstract methods inside abstract classes. – Bishoy Kamel Feb 02 '18 at 04:03
  • they have no implementation. that's why E is incorrect.I meant that normal class's methods can be overridden because they have a previous implementation. – Bishoy Kamel Feb 02 '18 at 04:06
  • Why do you feel the need to mention _"without overriding **existing implementation**"_? I feel like that's confusing the issue as you most certainly **can** override any implementations in the abstract class, you just don't have to – Phil Feb 02 '18 at 04:09
  • you are right. this can be confusing. I have edited my answer. – Bishoy Kamel Feb 02 '18 at 04:11