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
Recall & Review
beginner
What is partial abstraction in Java?
Partial abstraction means a class has some methods with implementation and some without. It is done using <code>abstract</code> classes that can have both abstract and concrete methods.
Click to reveal answer
beginner
How do you declare an abstract method in Java?
Use the abstract keyword before the method signature and do not provide a body. For example: abstract void display();
Click to reveal answer
beginner
Can you create an object of an abstract class?
No, you cannot create an object of an abstract class directly. You must create a subclass that implements the abstract methods and then create an object of that subclass.
Click to reveal answer
intermediate
Why use partial abstraction instead of full abstraction?
Partial abstraction lets you share common code in the abstract class while forcing subclasses to provide specific details. It helps avoid code duplication and provides a template for subclasses.
Click to reveal answer
beginner
Example of a partial abstraction in Java?
An abstract class Animal with an abstract method sound() and a concrete method sleep(). Subclasses like Dog implement sound().
Click to reveal answer
Which keyword is used to declare a class with partial abstraction in Java?
Aabstract
Bpartial
Cinterface
Dfinal
✗ Incorrect
The abstract keyword declares a class that can have both abstract and concrete methods.
Can an abstract class have implemented methods?
AYes, it can have both abstract and concrete methods
BNo, all methods must be abstract
COnly static methods can be implemented
DOnly private methods can be implemented
✗ Incorrect
Abstract classes can have both abstract methods (without body) and concrete methods (with body).
What happens if a subclass does not implement all abstract methods?
AIt will compile without errors
BIt becomes a concrete class
CIt must be declared abstract
DIt automatically implements methods
✗ Incorrect
If a subclass does not implement all abstract methods, it must also be declared abstract.
Which of these is NOT true about partial abstraction?
AIt uses abstract classes
BIt cannot be instantiated directly
CIt allows some methods to have code
DIt requires all methods to be abstract
✗ Incorrect
Partial abstraction means some methods have code; not all methods are abstract.
Why might you choose partial abstraction over interfaces?
ABecause interfaces cannot have methods
BTo provide common method implementations
CTo force all methods to be abstract
DTo allow object creation of the abstract class
✗ Incorrect
Partial abstraction allows sharing common code in abstract classes, unlike interfaces which traditionally only declare methods.
Explain partial abstraction in Java and how it differs from full abstraction.
Think about classes that have some methods with code and some without.
You got /5 concepts.
Describe a simple example of partial abstraction using an abstract class and a subclass.
Use an animal or vehicle example.
You got /4 concepts.
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
Step 1: Understand abstract class capabilities
Partial abstraction means an abstract class can have some methods with implementation (concrete) and some without (abstract).
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.
Final Answer:
An abstract class can have both abstract and concrete methods. -> Option A
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
Step 1: Recall abstract method syntax
Abstract methods must be declared with the 'abstract' keyword and no body, and usually have a visibility modifier.
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.
Final Answer:
public abstract void draw(); -> Option D
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
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'.
Step 2: Check output order
First 'a.sound()' prints 'Bark', then 'a.sleep()' prints 'Sleeping'.
Final Answer:
Bark
Sleeping -> Option C
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
Step 1: Check abstract method implementation
Vehicle declares abstract method move(). Any concrete subclass must implement it or be declared abstract.
Step 2: Analyze Car class
Car does not implement move() and is not abstract, so this causes a compilation error.
Final Answer:
Car class must implement the move() method or be abstract. -> Option A
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?
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
Step 1: Check abstract method implementation
Subclass must implement abstract method turnOn() to be concrete.
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.
Final Answer:
class Fan extends Appliance { void turnOn() { System.out.println("Fan is on"); } } -> Option B