πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

✏️

What Is an Abstract Class?

3 min readβ€’december 21, 2021

Milo Chang

Milo Chang

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

πŸŒ„Β Check out these other AP Computer Science A Resources:

What is an Abstract Class?

​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)

Example

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()"

Here's where abstract classes are different.

  • 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()"

Why use an abstract class in this situation?

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.

Important Points

  • 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)

Summary

  • 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)
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
πŸ™Exam Reviews