Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract methods

 
saiprasad raut
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In dan chisholm mock exam i came across following question

Which of the following statements is true?

a. An abstract method can not be overridden by an abstract method.
b. An instance method that is not abstract can not be overridden by an abstract method.
c. An abstract method declaration can not include a throws clause.
d. The body of an abstract method is represented by a set of empty brackets.
e. None of the above.

The answer is e.

How can a and b is false?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there an explanation given for the answers Dan provided? Perhaps he's looking at "overridden" in a slightly different way than I am.

I would say that A and B are correct answers, although A is rather pointless and B might be a questionable implementation and you'd have to back it up with good design decisions to ever do that in real life. However, for the sake of the SCJP exam, I'd say that both A and B are correct answers.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code example, the abstract method B.m1 overrides the abstract method A.m1. You might want to override an abstract method with another abstract method if you want to use the overriding declaration as a place to put new documentation that would appear in a javadoc.

The abstract method B.m2 overrides A.m2. It is unlikely that you would find something like that in the first revision of a product. It is more likely that you would find something like that in a later revision that extends the product to do things that were previously not envisioned by the original design team.

The following code compiles without error.

 
saiprasad raut
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Corey and Dan for the replies.

So for the exam, should i go ahead with the consideration that a and b is false?

By the example of Dan it seems so...

Saiprasad
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, no. A & B are true, not false.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by saiprasad raut:
Thanks Corey and Dan for the replies.

So for the exam, should i go ahead with the consideration that a and b is false?

By the example of Dan it seems so...

Saiprasad


Yes, the examples above show that an abstract method can override a method of a superclass. The overridden superclass method could be either concrete or could be abstract.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course unless the concrete method is final...
Compiler doesn't complain about this:

for this implementation of Test1:

but is rejected when Test1 is this:

Question remains what the point in overriding an concrete method with an abstract method actually is.
Given that derived classes should provide more (specific) behaviour compared to their parents it seems to go against OO principles to actually remove functionality by overriding a concrete method with an abstract one.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeroen Wenting:

...

Question remains what the point in overriding an concrete method with an abstract method actually is.
Given that derived classes should provide more (specific) behaviour compared to their parents it seems to go against OO principles to actually remove functionality by overriding a concrete method with an abstract one.



The new subclass that contains the overriding abstract method would be an abstract class. The abstract class could not be instantiated. Instead, the abstract class would just serve as an abstract superclass that requires any concrete subclass to provide a new implementation of the abstract method.

As I mentioned earlier, it is unlikely that you would find something like that in the first revision of a product. It is more likely that you would find something like that in a later revision where new functionality is has been added that was not envisioned by the original design team.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic