Bird
Raised Fist0
Javaprogramming~10 mins

Partial abstraction in Java - Step-by-Step Execution

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
Concept Flow - Partial abstraction
Define Abstract Class
Declare Abstract and Concrete Methods
Create Subclass
Implement Abstract Methods
Instantiate Subclass
Use Methods (Concrete + Implemented Abstract)
End
Partial abstraction means a class has some methods with no body (abstract) and some with body (concrete). Subclasses must implement the abstract methods.
Execution Sample
Java
abstract class Animal {
  void breathe() { System.out.println("Breathing"); }
  abstract void sound();
}
class Dog extends Animal {
  void sound() { System.out.println("Bark"); }
}
public class Main {
  public static void main(String[] args) {
    Dog d = new Dog();
    d.breathe();
    d.sound();
  }
}
Defines an abstract class with one concrete and one abstract method; subclass implements abstract method; calls both methods.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with breathe() and abstract sound()Class Animal createdAnimal has one concrete and one abstract method
2Define subclass Dog extends AnimalClass Dog createdDog inherits breathe(), must implement sound()
3Implement sound() in DogMethod sound() implementedDog now concrete class
4Create Dog instance dDog d = new Dog()Object d created
5Call d.breathe()Calls concrete method in AnimalPrints: Breathing
6Call d.sound()Calls implemented method in DogPrints: Bark
7EndProgram endsAll methods executed successfully
💡 Program ends after calling both methods on Dog instance
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6Final
dnullDog object createdDog object existsDog object existsDog object exists
Key Moments - 3 Insights
Why can't we create an instance of the abstract class Animal?
Because Animal has an abstract method without implementation, Java prevents creating its objects. See execution_table step 4: only Dog (concrete subclass) can be instantiated.
What happens if Dog does not implement the abstract method sound()?
Dog would also become abstract and cannot be instantiated. The program would not compile. This is why in step 3 Dog implements sound().
Why can we call breathe() on Dog even though it's defined in Animal?
Because breathe() is a concrete method in Animal, Dog inherits it directly. Step 5 shows calling breathe() on Dog instance works fine.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed when d.breathe() is called at step 5?
ABark
BSound
CBreathing
DNothing
💡 Hint
Check execution_table row for step 5 under Result column
At which step is the Dog object created?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at execution_table rows and find where 'Dog d = new Dog()' happens
If Dog did not implement sound(), what would happen?
ADog would be abstract and cannot be instantiated
BDog would inherit sound() from Animal
CProgram would print 'Bark' anyway
DDog would automatically implement sound()
💡 Hint
Refer to key_moments about implementing abstract methods
Concept Snapshot
Partial abstraction means a class has some methods with bodies and some without (abstract).
Abstract classes cannot be instantiated directly.
Subclasses must implement all abstract methods.
Concrete methods in abstract classes are inherited as is.
This allows sharing common code and forcing method implementation.
Full Transcript
Partial abstraction in Java means creating an abstract class that has both concrete methods (with code) and abstract methods (without code). The abstract class cannot be instantiated directly because it has incomplete methods. A subclass extends this abstract class and must provide code for all abstract methods. Once the subclass implements these methods, it becomes a concrete class and can be instantiated. The subclass inherits concrete methods from the abstract class and can use them directly. In the example, Animal is abstract with breathe() concrete and sound() abstract. Dog extends Animal and implements sound(). We create a Dog object and call both breathe() and sound(), printing 'Breathing' and 'Bark' respectively. This shows how partial abstraction allows code reuse and enforces method implementation.

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