Design classes that cannot be instantiated but serve as strict templates for others.
Imagine you are designing a video game with hundreds of different monsters. You know every monster must 'attack' and 'move,' but a ghost moves differently than a dragon. How do you force every monster to have these abilities without writing the same code over and over—or worse, forgetting one?
In Object-Oriented Programming, an Abstract Class is a class that cannot be instantiated. This means you cannot use the `new` keyword to create an object from it directly. Think of it as a 'half-finished' blueprint. It defines what a group of related objects should do, but it leaves the specific details to its children. We use the abstract keyword to tell the compiler: 'This class is an idea, not a finished product.' This is powerful because it allows us to define common properties (like a monster's or ) while forcing subclasses to define their own unique behaviors.
1. Define an abstract class `Shape`. 2. Add a concrete property: `String color`. 3. Add an abstract method: `abstract double calculateArea();`. 4. Because every shape has an area, but the formula for a circle () is different from a square (), we leave the method body empty in the parent class.
Quick Check
If you have an abstract class named 'Animal', can you execute the code: 'Animal myPet = new Animal();'?
Answer
No, abstract classes cannot be instantiated directly.
When a subclass extends an abstract class, it enters into a 'contract.' It must provide a concrete implementation for every abstract method defined in the parent. If it fails to do so, the subclass itself must be declared abstract, and the code will not compile. This ensures consistency across your software. For example, if your `Payment` template has an abstract method `processTransaction()`, the `CreditCard` and `PayPal` subclasses are forced to write that logic, preventing bugs where a developer might forget to include a critical function.
1. Create an abstract class `Employee` with a method `calculateMonthlyPay()`. 2. For a `FullTimeEmployee`, the implementation is . 3. For a `Contractor`, the implementation is . 4. By using an abstract parent, you can create a list of `Employee` objects and call `calculateMonthlyPay()` on all of them without knowing their specific type.
Quick Check
What happens if a concrete subclass fails to implement an abstract method from its parent?
Answer
The code will result in a compilation error.
A common point of confusion is when to use an Abstract Class versus an Interface. Use an abstract class when you want to share state (variables like `int health`) and common code (methods that work the same for everyone). Use an interface when you want to define a capability (like `IDamageable` or `ISerializable`) that could apply to totally unrelated classes. A `Bird` and a `Plane` both `Fly()`, but they aren't the same 'kind' of thing. A `Bird` would extend an abstract `Animal` class but implement a `Flyable` interface.
Imagine a game with a class hierarchy: 1. Abstract Class `GameObject`: Contains coordinates and a concrete method `moveTo(x, y)`. 2. Interface `IDestructible`: Contains an abstract method `takeDamage(int amount)`. 3. The `Player` class extends `GameObject` (because it has a position) and implements `IDestructible` (because it can be hurt). 4. A `Cloud` class might only extend `GameObject` because it moves but cannot be destroyed.
Which keyword is used to prevent a class from being instantiated while allowing it to be inherited?
If class is abstract and contains an abstract method , and class extends , what must do to be a concrete class?
An abstract class can contain both abstract methods and regular (concrete) methods.
Review Tomorrow
In 24 hours, try to explain the difference between an 'is-a' relationship (Abstract Classes) and a 'can-do' relationship (Interfaces) to a peer.
Practice Activity
Create a small program for a Library system. Use an abstract class 'LibraryItem' with an abstract method 'getDueDate()' and subclasses 'Book' and 'DVD'.