Reputation: 2679
I'm almost taking the Java SE 8 Programmer I exam (1Z0-808). I'm using this study guide: https://www.selikoff.net/java-oca-8-programmer-i-study-guide/. When answering the review questions in chapter 5 (Class Design) i failed at this question:
Which of the following is true about a concrete subclass? (Choose all that apply)
My answers were 2 and 5. But only the 2nd was correct. I selected the 5th answer because i thought that it is true you cannot override an abstract method from an abstract class, but you can implement it, like inferfaces, which are almost like abstract classes since Java 8.
Knowing that interfaces abstract methods are implemented, not overriden, when talking about abstract classes: is it correct to say "Abstract methods can be overriden by a concrete subclass" instead of "Abstract methods can be implemented by a concrete subclass"?
If we pay attention to the second answer (which is the right one) they used the word "implement".
Upvotes: 2
Views: 2286
Reputation: 6752
- A concrete subclass can be declared as abstract.
This is false due to wording: a concrete calss can not be abstract, if it is a subclass and abstract, it is thus an abstract subclass.
- A concrete subclass must implement all inherited abstract methods.
This is actually a trick question. A subclass does not have to implement any abstract methods, however, if it does not, it too must be declared as an abstract class. So as with question 1, it's wording; it's not technically a concrete subclass if it is abstract, so this question would be true.
- A concrete subclass must implement all methods defined in an inherited interface.
This too is true. However, if the subclass inherits from a superclass which already implements the interface, then the subclass does not have to re-implement them, in which case it would be false.
- A concrete subclass cannot be marked as final.
This is false; of course it can.
- Abstract methods cannot be overridden by a concrete subclass.
This is another wording issue. Technically you are not overriding an abstract method, you're implementing it, so this would be false. Overriding a method means that the method you are overriding has an exact match (name, return type, parameters) in it's parent class. Hiding methods is the same as overriding them, except hiding only applies to static methods and fields. So if a method has a body, and you implement the same method in a subclass, you're overriding it. If you implement an abstract method in a subclass, you're merely implementing it. If you implement a static method in a subclass, you're hiding it.
These questions are worded horribly to try and convey they idea of an abstract class, interfaces and heritability. Though, given that development is about attention to detail, maybe that's more the point ..
Hope that can help clear some things up.
Upvotes: 0
Reputation: 60927
The Java Language Specification does not define the concept of a "concrete class". It does talk about concrete methods, where a concrete method has a defined implementation (as opposed to an abstract method which has no body, only a type signature). Java 8's default methods in interfaces muddy the waters here a little, but not entirely: even if a method has a default implementation, it is still considered abstract (the default implementation is simply copied into the implementing class if no other implementation is provided).
Since the notion of a concrete class is ill-defined, it is difficult to make any statements about it with certainty. Generally when people talk about a concrete class they simply mean a non-abstract class, but I have also seen (and used myself) the expression "concrete type" to refer to a specific instantiation of a generic type. For example, List<T>
is generic, but List<String>
could be considered a concrete counterpart to List<T>
even though List
is still an abstract type (since it is an interface).
Additionally, since a class can have multiple independent abstract interface inheritance hierarchies, it can be simultaneously concrete for some of them and yet abstract for others!
The short answer: when faced with a test designed by people who don't understand the subject matter themselves, it becomes meaningless to obsess about which answer is "correct". Learn from the specification, as it is the canonical definition of what is correct in the language.
Upvotes: 0
Reputation: 37835
This is how the specification uses this terminology:
If a non-
abstract
methodmC
overrides an abstract methodmA
from a classC
, thenmC
is said to implementmA
fromC
.An instance method
mC
declared in or inherited by classC
, overrides fromC
another methodmI
declared in an interfaceI
, iff all of the following are true: [...]
Note that it does use the term "overrides" in reference to overriding interface methods.
In more plain terms, if a method overrides an abstract method (either from an abstract class or interface), then the overriding method implements the abstract method. It's still considered an override, though.
abstract class A {
abstract void m();
void n() {}
}
class C extends A {
// C.m() both overrides and implements A.m()
@Override
void m() {}
// C.n() overrides A.n(), but does not implement it
@Override
void n() {}
}
Upvotes: 3