Bird
Raised Fist0
Javaprogramming~10 mins

Partial abstraction in Java - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an abstract class named Vehicle.

Java
public [1] class Vehicle {
    // class body
}
Drag options to blanks, or click blank then click option'
Aprivate
Bfinal
Cstatic
Dabstract
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'final' instead of 'abstract' will prevent the class from being subclassed.
Using 'static' or 'private' keywords here is incorrect for class declaration.
2fill in blank
medium

Complete the code to declare an abstract method named startEngine in the Vehicle class.

Java
public abstract class Vehicle {
    public [1] void startEngine();
}
Drag options to blanks, or click blank then click option'
Aabstract
Bstatic
Cfinal
Dprivate
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'final' or 'static' keywords with abstract methods is not allowed.
Forgetting the semicolon at the end of the method declaration.
3fill in blank
hard

Fix the error in the subclass Car that extends Vehicle by correctly overriding the abstract method startEngine.

Java
public class Car extends Vehicle {
    @Override
    public void [1] {
        System.out.println("Engine started");
    }
}
Drag options to blanks, or click blank then click option'
AstartEngine()
BstartEngine(void)
CstartEngine
DstartEngine[]
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Omitting parentheses after the method name causes a syntax error.
Using incorrect parameter syntax like 'void' inside parentheses.
4fill in blank
hard

Fill both blanks to declare an abstract class Shape with an abstract method draw.

Java
public [1] class Shape {
    public [2] void draw();
}
Drag options to blanks, or click blank then click option'
Aabstract
Bfinal
Dstatic
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'final' or 'static' keywords instead of 'abstract' for class or method.
Providing a method body for an abstract method.
5fill in blank
hard

Fill all three blanks to create a subclass Circle that extends Shape and implements the draw method.

Java
public class Circle extends [1] {
    @Override
    public void [2] {
        System.out.println([3]);
    }
}
Drag options to blanks, or click blank then click option'
AShape
Bdraw()
C"Drawing a circle"
DShape()
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using constructor syntax 'Shape()' instead of class name in extends.
Omitting parentheses in method override.
Forgetting to put the message inside quotes.

Practice

(1/5)
1.

What does partial abstraction mean in Java?

easy
A. An abstract class can have both abstract and concrete methods.
B. An abstract class can only have abstract methods.
C. An abstract class can be instantiated directly.
D. All methods in an abstract class must be static.

Solution

  1. Step 1: Understand abstract class capabilities

    Partial abstraction means an abstract class can have some methods with implementation (concrete) and some without (abstract).
  2. Step 2: Compare options with definition

    Only An abstract class can have both abstract and concrete methods. correctly states this; others are incorrect or false statements.
  3. Final Answer:

    An abstract class can have both abstract and concrete methods. -> Option A
  4. Quick Check:

    Partial abstraction = abstract + concrete methods [OK]
Hint: Abstract class can mix method types, not only abstract [OK]
Common Mistakes:
  • Thinking abstract classes must have only abstract methods
  • Believing abstract classes can be instantiated
  • Confusing static methods with abstract methods
2.

Which of the following is the correct way to declare an abstract method inside an abstract class?

public abstract class Shape {
    ?
}
easy
A. public void draw();
B. void draw() {}
C. abstract void draw();
D. public abstract void draw();

Solution

  1. Step 1: Recall abstract method syntax

    Abstract methods must be declared with the 'abstract' keyword and no body, and usually have a visibility modifier.
  2. Step 2: Check each option

    public abstract void draw(); correctly declares 'public abstract void draw();'. public void draw(); misses 'abstract', C misses visibility, D has a method body which is invalid for abstract methods.
  3. Final Answer:

    public abstract void draw(); -> Option D
  4. Quick Check:

    Abstract method = 'public abstract' + no body [OK]
Hint: Abstract methods have no body and use 'abstract' keyword [OK]
Common Mistakes:
  • Omitting the 'abstract' keyword
  • Providing a method body for abstract methods
  • Missing visibility modifier
3.

What will be the output of the following code?

abstract class Animal {
    abstract void sound();
    void sleep() {
        System.out.println("Sleeping");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
        a.sleep();
    }
}
medium
A. Compilation error
B. Sleeping\nBark
C. Bark\nSleeping
D. Runtime error

Solution

  1. Step 1: Understand method calls on abstract class reference

    Variable 'a' is of type Animal but refers to Dog instance. Calling 'sound()' invokes Dog's override, printing 'Bark'. Calling 'sleep()' uses Animal's concrete method, printing 'Sleeping'.
  2. Step 2: Check output order

    First 'a.sound()' prints 'Bark', then 'a.sleep()' prints 'Sleeping'.
  3. Final Answer:

    Bark Sleeping -> Option C
  4. Quick Check:

    Abstract ref calls subclass method then superclass concrete method [OK]
Hint: Abstract class ref calls subclass override methods [OK]
Common Mistakes:
  • Expecting abstract class to instantiate directly
  • Confusing method call order
  • Thinking abstract methods have implementation
4.

Identify the error in the following code snippet:

abstract class Vehicle {
    abstract void move();
}

class Car extends Vehicle {
    void start() {
        System.out.println("Car started");
    }
}
medium
A. Car class must implement the move() method or be abstract.
B. Vehicle class cannot have abstract methods.
C. Car class cannot have methods other than move().
D. No error, code is correct.

Solution

  1. Step 1: Check abstract method implementation

    Vehicle declares abstract method move(). Any concrete subclass must implement it or be declared abstract.
  2. Step 2: Analyze Car class

    Car does not implement move() and is not abstract, so this causes a compilation error.
  3. Final Answer:

    Car class must implement the move() method or be abstract. -> Option A
  4. Quick Check:

    Concrete subclass must implement all abstract methods [OK]
Hint: Concrete subclass must implement all abstract methods [OK]
Common Mistakes:
  • Forgetting to implement abstract methods in subclass
  • Thinking abstract class can't have abstract methods
  • Believing subclass can skip abstract methods without being abstract
5.

Given the abstract class below, which subclass implementation correctly uses partial abstraction?

abstract class Appliance {
    abstract void turnOn();
    void plugIn() {
        System.out.println("Plugged in");
    }
}

Choose the correct subclass:

hard
A. class Fan extends Appliance {}
B. class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } }
C. class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } void plugIn() { System.out.println("Fan plugged in"); } }
D. class Fan extends Appliance { void plugIn() { System.out.println("Fan plugged in"); } }

Solution

  1. Step 1: Check abstract method implementation

    Subclass must implement abstract method turnOn() to be concrete.
  2. Step 2: Analyze each subclass option

    class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } } implements turnOn() only, using inherited plugIn() as is, which is valid partial abstraction. class Fan extends Appliance { void plugIn() { System.out.println("Fan plugged in"); } } misses turnOn(), causing error. class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } void plugIn() { System.out.println("Fan plugged in"); } } overrides plugIn() unnecessarily but is valid. class Fan extends Appliance {} misses turnOn(), causing error.
  3. Final Answer:

    class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } } -> Option B
  4. Quick Check:

    Implement abstract methods, inherit concrete ones [OK]
Hint: Implement abstract methods, inherit concrete ones unchanged [OK]
Common Mistakes:
  • Not implementing abstract methods in subclass
  • Overriding concrete methods unnecessarily
  • Leaving subclass empty without abstract method implementation