The Abstract Class
What keyword allows a subclass to inherit an abstract class?
"extends" Eg. public class Circle extends Shape {public Circle (double r) { radius = r; public double getArea(){ return Math.PI * radius^2; } private double radius; }
What is an abstract method?
A "template" for a method that concrete subclasses must implement. This means all concrete subclasses are guaranteed to honor the template inherited from the abstract class.
What is an abstract class?
A class which cannot be instantiated. It may or may not contain one or more abstract methods.
Summary
An abstract class is a class which cannot be instantiated. It is used by creating an inheriting subclass that can be instantiated. An abstract class does a few things for the inheriting subclass: 1. Defines methods which can be used (and must be defined) by the inheriting subclass. 2. Provides a common interface that groups subclasses together.
How do you declare an abstract class? How about an abstract method?
Eg. abstract public class Shape { public abstract double getArea(); }
Give an example of an abstract class.
Eg. An abstract class "Shape" with a method "area." If Circle, Square, and Rectangle extend "Shape," they will inherit the "area" method which each calculates differently.
Can you instantiate an abstract class? (Can you create an instance of one?)
No. Abstract classes may not be instantiated directly; only their concrete subclasses are instantiable.
T/F: A class may be declared abstract even if it has no abstract methods.
True. And this will prevent that class from being instantiated. [Eg. This could be useful if a base class in some hierarchy has no abstract methods but is not itself meant to be instantiated.]
Why use an abstract class?
Usually, there is a category of objects that have a common behavior, but the details of calculating or performing those functions is different for each object.