nerdexam
Oracle

1Z0-819 · Question #136

Given: 1. interface Pastry { 2. void getIngredients(); 3. } 4. abstract class Cookie implements Pastry {} 5. 6. class chocolateCookie implements Cookie { 7. public void getIngredients() {} 8. } 9…

The correct answer is A. The compilation fails due to an error in line 6. Option A is correct because Cookie is an abstract class, not an interface, and Java only allows implements to be used with interfaces. Line 6 should read class chocolateCookie extends Cookie - using implements with a class is a compile-time error, regardless of whether the…

Java Object-Oriented Approach

Question

Given:
  1. interface Pastry {
  2. void getIngredients();
  3. }
  4. abstract class Cookie implements Pastry {}
  5. class chocolateCookie implements Cookie {
  6. public void getIngredients() {}
  7. }
  8. class CoconutChocolateCookie extends ChocolateCookie {
  9. void getIngredients(int X) {}
  10. }
Which is true?

Options

  • AThe compilation fails due to an error in line 6.
  • BThe compilation succeeds.
  • CThe compilation fails due to an error in line 4.
  • DThe compilation fails due to an error in line 10.
  • EThe compilation fails due to an error in line 7.
  • FThe compilation fails due to an error in line 2.
  • GThe compilation fails due to an error in line 9.

How the community answered

(54 responses)
  • A
    81% (44)
  • B
    6% (3)
  • C
    2% (1)
  • F
    2% (1)
  • G
    9% (5)

Explanation

Option A is correct because Cookie is an abstract class, not an interface, and Java only allows implements to be used with interfaces. Line 6 should read class chocolateCookie extends Cookie - using implements with a class is a compile-time error, regardless of whether the class is abstract or concrete.

Why the distractors are wrong:

  • C (line 4) is valid - abstract classes are permitted to implement an interface without providing implementations for its methods; that obligation passes down to concrete subclasses.
  • E (line 7) is valid - public void getIngredients() correctly implements the interface contract with equal or wider visibility.
  • F (line 2) is valid - interface methods are implicitly public abstract, so the declaration is legal.
  • D (line 10) shows an overload (different parameter), not an error per se, but the code never reaches that point due to the earlier failure.
  • G (line 9) references ChocolateCookie (capital C) while line 6 defines chocolateCookie (lowercase c) - this would also be an error, but the compiler halts at line 6 first.
  • B is simply wrong because the code does not compile.

Memory tip: Think of the keywords by relationship type - extends is for class-to-class (one "extends" the family tree), implements is strictly for class-to-interface. If you ever see a class implements another class, flag it immediately.

Topics

#interface vs abstract class#implements vs extends#inheritance#compilation error

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice