nerdexam
Oracle

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.

Working with Inheritance

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)
  • B
    6% (2)
  • C
    3% (1)
  • D
    85% (29)
  • E
    6% (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.

Apublic abstract class Car {

The method `stop()` is declared without a body and without the `abstract` keyword, making it an illegal declaration for a non-abstract method.

Bpublic interface Car {

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.

Cpublic abstract class Car {

A method cannot be both `final` (cannot be overridden) and implicitly `abstract` (requires implementation) by lacking a body, as these keywords are contradictory.

Dpublic abstract class Car {Correct

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.

Epublic abstract class Car {

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

#abstract class#inheritance#class declaration

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice