Yorkville High School Computer Science Department
Yorkville High School Computer Science Department on Facebook  Yorkville High School Computer Science Department Twitter Feed  Yorkville High School Computer Science Department on Instagram

Yorkville High School Computer Science

ASSIGNMENTS: Hidden Figure - May 24, 2019

AP Computer Science :: Lessons :: Abstract Classes and Interfaces

Barron's AP Computer Science

Chapter 3:
Pages 196 - 202

Fundamentals of Java
Chapter 10
Pages 359 - 366, 372 - 380

Abstract Classes

An abstract class is a superclass that represents some general concept that should not be instantiated. For an example, download the Shapes example from the Code Repository, which we will use for the rest of this lesson.

An abstract class is declared by placing the keyword abstract in the class header like so:

public abstract class AbstractClassName

The keywords extends is used to specify a subclass of the abstract class since it is simply a special type of inheritance relationship.

public class SubClassName extends AbstractClassName

If you look at the Shape example you will see there is very little in the superclass Shape. It has a declaration for the variable myName, a constructor, and an accessor for myName. It also has three methods. One method calculates the semiperimeter, which will always be half the perimeter. The other two methods are abstract because they have no implementation. Calculating the area and perimeter may be different for different shapes so even though we need to be able to calculate the area and perimeter for every shape the implementation is left up to the subclasses.

It is important to note that even though you cannot declare an instance of an abstract class you can still use the principles of polymorphism when using these classes. The following lines of code would all be valid:

Shape c = new Circle(1.5, "small circle");
Shape circ = new Circle(10, "circle");
Shape square = new Square(9.4 "square");
Shape s = null;


An interface is a collection of related methods whose headers are provided without any implementation. How is this different from an abstract class? An interface cannot have any implementation so it does not contain instance variables or methods that contain any nonheader code. Here are some of the differences and similarities between abstract classes and interfaces:

An example of an interface would be a FlyingObject interface that contains a method called fly() that makes the object fly and an isFlying() method that returns true if the object is flying. This interface could be implemented for a class using the implements keyword as shown below:

public class Bird implements FlyingObject

The Bird class must implement fly() and isFlying() since they were declared in the interface. You can also extend a superclass and implement an interface as shown below:

public class Mosquito extends Insect implements FlyingObject

Finally, you can only have a single superclass for any given class, but that class can implement any number of interfaces.

The Comparable Interface

The Comparable Interface is included in the standard java.lang package so there is no need to import it. The Comparable Interface provides a useful method for comparing objects that returns an integer. You will recognize this method from String comparisons since the String class implements the Comparable Interface. Below is the entirety of the interface:

public interface Comparable
   int compareTo(Object obj);

You can look at the Shape class for an example of a class that implements the Comparable interface.

Yorkville High School Computer Science Department on Facebook Yorkville High School Computer Science Department Twitter Feed Yorkville High School Computer Science Department on Instagram