About Question enthuware.ocajp.i.v7.2.1300 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
WitanRodrigues
Posts: 1
Joined: Wed Aug 14, 2013 7:53 am
Contact:

About Question enthuware.ocajp.i.v7.2.1300 :

Post by WitanRodrigues »

The question asserts: "An abstract method cannot be overridden." and the answer is 'false'

But I found this in the book Sun Certified Java Programmer 6:
"technically, abstract methods must be implemented, as opposed to overriden, since there really isn't anything to override." - by Kathy Sierra and Bert Bates


So, is the answer to this question subjective? May the answer be true?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1300 :

Post by admin »

I agree that it could be a little subjective but if we change the answer to true, then it may be even more wrong because only final (and private) methods cannot be overridden. An abstract method is meant to be implemented by the subclass method and a sub-subclass can override it (unless the subclass makes it final).

Also, when you implement the abstract method in a subclass, you are kind of overriding the behaviour of the original method in the sense that it was not implemented in the base class and now it is.
If you had a final abstract method (hypothetically, because you cannot mark an abstract method final), you could not even change the behavior that it is not implemented.

Overall, I agree that it is not a very clear choice.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

chriswilty
Posts: 1
Joined: Mon Dec 09, 2013 2:50 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1300 :

Post by chriswilty »

I agree with WitanRodrigues (and Kathy Sierra and Burt Bates): there is nothing to override if there is no implementation. Considering that we are supposed to memorise and adopt the highly subjective semantics of the OCAJP test authors (such as "overloaded" and "default constructor") then I think the least we can expect is a consistent definition of "override".

This kind of ambiguous question can be frustrating and demoralising for people studying for the exam, and should be removed from your test set.

Kevin_C
Posts: 14
Joined: Mon Nov 03, 2014 5:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1300 :

Post by Kevin_C »

As a reply to the rest: When you "override" an abstract method, there is indeed nothing to override. However, Java doesn't make a difference in this, it's both used as an Override and you can also add an @Override annotation to both an overridden method with a normal class as superclass, or with an abstract class as superclass. Though by logically thinking there obviously is a difference, since there is nothing to override when nothing doesn't exist yet, in Java it is counted as the same in terms of overriding.

You could also (kinda) look at it this way: Abstract methods are similar to empty methods.
In a normal class:

Code: Select all

public void someMethod(){ }
vs In an abstract class:

Code: Select all

public abstract void someMethod();
Both can be overridden the same way:

Code: Select all

@Override // <- This is optional to add.
public void someMethod(){
   ... // some code
}
As a quote by Dave L. from this stackoverflow post regarding the @Override annotation: "I think it would be better if Java had a separate annotation (like @Implements), but using @Override for both is better than nothing."

Note: @Override is optional to add. During compile time it will check if there is a method with the same structure/name as this method to override, if not, it gives a compile time error. By this you can make sure you override the method instead of creating a new method. For example, you've created your own class (automatically extending from Object) and want to override the toString method:

Code: Select all

// This will correctly override Object's toString:
public String toString(){
   return "some string";
}

// This will make a separate method in your class that can be called like instanceOfYourClass.tostring() (note the lower case s of string):
public String tostring(){
   return "some string";
}

// This will again correctly override Object's toString:
@Override
public String toString(){
   return "some string";
}

// This will give a compile time error, since there is no tostring method (with lower case s) in the superclass Object:
@Override
public String tostring(){
   return "some string";
}
I know annotations are outside the scope for the exam, but it's still an interesting feature of Java that I'd like to share for those who are interested.

vstefanyuk
Posts: 1
Joined: Tue Feb 16, 2016 7:27 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1300 :

Post by vstefanyuk »

Another thing is - it is possible actually to override returning type without implementing method:

abstract class A {
abstract Object a();
}
abstract class B extends A {
@Override
abstract String a();
}

In my opinion it is, actually, override, so this test question is not that clear.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests