🌄 Check out these other AP Computer Science A Resources:
An abstract class is a "root" class that you can derive other classes from. You use them when there are several classes that will have similar methods available. If there is even one abstract method in a class, you must make that class an abstract class!
Abstract classes cannot be instantiated; you can't create an object of an abstract class. You can't do "AbstractClass a = new AbstractClass();". 🚫
In an abstract class, you can have constructors. You can also define methods and variables that the child classes will inherit.
Inside an abstract class, you can have regular methods or abstract methods.
- A regular method is written just like an ordinary method ✔
- An abstract method must be left with no body (no braces, just put a semicolon after the method header)
- An abstract method is basically a promise that the method will exist and be implemented in a child class
- If you have an abstract method, any regular class derived from your abstract class has to implement the abstract method (you have to give the method a body)
public abstract class Shape
{
protected String name;
public Shape()
{
name = "";
}
public String toString()
{
return name;
}
public abstract double getArea();
}
*** Credit to my AP CS A teacher Mr. Kim for this example ***
For the most
part, everything in this abstract class seems to be what would be in a regular class.
- The line "protected String name;" creates a String variable that will be called name
- There is a constructor in this example, which sets name equal to an empty String
- There is also a regular method in this abstract class: "public String toString()"
- You may notice that "public abstract double getArea();" looks very different from a regular method
- There's the keyword "abstract" there now
- There's no braces after the method header, just a semicolon
- The abstract method "getArea" promises that all non-abstract child classes of the Shape class will have a method "public double getArea()"
You can find the area of different types of shapes (circles, triangles, rectangles, squares, etc.) but they each have different formulas. By using an abstract class, you guarantee that every child of the Shape class (like a Triangle class) will have a method called "getArea". Each child class is free to have its own formula to calculate the area.
- A child class of an abstract class will not compile if you do not implement the abstract methods (the only exception is if the child class is also abstract)
public class Circle extends Shape
{
*** The variable "name" is inherited from the parent class, Shape ***
private double r;
public Circle (double x)
{
r = x;
name = "Circle";
}
public double getArea()
{
return MATH.PI*r*r;
// Without this code to implement the method, this class will not
// compile
}
}
*** Credit to my AP CS A teacher Mr. Kim for this example ***
- Note that the child class "extends" the abstract parent class (just like a regular child class "extends" a regular parent class)
- A child class can only inherit one abstract class (this is different from interfaces)
- Not all methods in an abstract class have to be abstract (this is different from interfaces)
- Variables (also called fields) in an abstract class could be static variables (meaning they belong to the class) or member variables (belonging to an object)
- A class with at least one abstract method must be an abstract class
- An abstract class cannot be instantiated
- Abstract classes can have non-abstract methods and other code similar to regular classes
- A non-abstract child class of an abstract class must implement all of the abstract methods
- A child class can only inherit one abstract class
- For an abstract method, just put a semicolon after the method header (no braces)