1Z0-803 · Question #74
Which is a valid abstract class?
The correct answer is D. public abstract class Car {. A valid abstract class can define both abstract methods (without a body) and concrete methods (with a body), adhering to specific keywords for each type of method.
Question
Which is a valid abstract class?
Options
- Apublic abstract class Car {
- Bpublic interface Car {
- Cpublic abstract class Car {
- Dpublic abstract class Car {
- Epublic abstract class Car {
How the community answered
(34 responses)- B6% (2)
- C3% (1)
- D85% (29)
- E6% (2)
Why each option
A valid abstract class can define both abstract methods (without a body) and concrete methods (with a body), adhering to specific keywords for each type of method.
The method `stop()` is declared without a body and without the `abstract` keyword, making it an illegal declaration for a non-abstract method.
In an interface, methods with bodies, like `start()`, must be declared with the `default` or `static` keyword (from Java 8 onwards); otherwise, all methods are implicitly abstract and cannot have a body.
A method cannot be both `final` (cannot be overridden) and implicitly `abstract` (requires implementation) by lacking a body, as these keywords are contradictory.
This abstract class correctly defines two abstract methods, `drive()` and `stop()`, without implementations, and one concrete method, `start()`, with an implementation. All methods adhere to Java's rules for abstract and concrete method declarations within an abstract class.
A `static` method must provide an implementation (a method body); it cannot be declared without a body, as `static` implies it belongs to the class and doesn't rely on instance state.
Concept tested: Java abstract classes and method types
Source: https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html
Topics
Community Discussion
No community discussion yet for this question.