0
0
Javaprogramming~10 mins

Partial abstraction in Java - Step-by-Step Execution

Choose your learning style9 modes available
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.