Sub topic 2
Declaring an Abstract Class
An abstract class is a class marked with the abstract keyword. It describes a general idea, such as "an animal" or "a shape," that is too broad to exist on its own. You can't create an object from it directly. Other classes extend it and fill in the details.
Why Declare a Class Abstract?
Some classes exist only to be parents. Think about a zoo program. Every real animal is a dog, a cat, or a lion. Nobody owns a plain "animal" that is no animal in particular. If you mark Animal as abstract, Java stops anyone from making that kind of object by mistake.
An abstract class still does real work:
- It groups code that all its child classes share.
- It gives those child classes a common type.
- It stops anyone from creating an object of the general idea.
The Syntax
To make a class abstract, put abstract before the word class:
abstract class Animal {
public String sleep() {
return "Zzz";
}
}
class Dog extends Animal {
}
public class Main {
public static void main(String[] args) {
Animal pet = new Dog();
System.out.println(pet.sleep()); // prints: Zzz
// Animal a = new Animal(); // compile error: Animal is abstract
}
}Here is what each part does:
abstract class Animaldeclares the abstract class.sleep()is a concrete method, which is a method with a body. Abstract classes can have these, just like normal classes.Dog extends Animalcreates a child class.Doggetssleep()for free.new Dog()works becauseDogis not abstract.new Animal()fails. The compiler, which is the tool that turns your code into a runnable program, rejects it.
What Happens If You Try to Instantiate It?
To instantiate a class means to create an object from it with new. If you uncomment the last line in main, the compiler stops with an error like this:
error: Animal is abstract; cannot be instantiatedThis is a compile-time error. It shows up before the program runs, so the mistake never reaches users.
Rules to Remember
| Question | Answer |
|---|---|
| Can it have fields and normal methods? | Yes |
| Can you call `new` on it? | No |
| Can a class extend it? | Yes, with `extends` |
| Can a variable have its type? | Yes |
| Must it contain abstract methods? | No, but it usually does |
In the next chapter you will meet abstract methods. They are methods with no body that child classes must write. They give abstract classes most of their usefulness.
Exercise
You have abstract class Vehicle {} and class Bike extends Vehicle {}. Which of these lines compile? (a) Vehicle v = new Bike(); (b) Vehicle v = new Vehicle(); (c) Bike b = new Bike();
Show answer
Lines (a) and (c) compile. Line (b) fails because Vehicle is abstract and cannot be instantiated with new. Line (a) works because a variable may have an abstract type as long as the object is a non-abstract class.
Exercise
Write an abstract class Shape with a concrete method String label() that returns "shape". Then write a class Square that extends it, and print the label from a Square object.
Show answer
abstract class Shape { public String label() { return "shape"; } } class Square extends Shape {} and in main: System.out.println(new Square().label()); which prints: shape