AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Class Design
  • Abstract Classes
  • Creating an Abstract Class
  • Creating Abstract Methods

Was this helpful?

  1. Classes and Object-Oriented Programming

Class Design and Abstract Classes

PreviousInheritanceNextPolymorphism

Last updated 5 years ago

Was this helpful?

One of the biggest challenges in Java is taking the time to actually write out relations between classes, outside of the editor. It is incredibly important to understand how each class relates to one another. It is also important to know the characteristics that define each class.

Class Design

Let's take a look at the Vehicle Class from the previous chapter:

In this diagram we can see that both Car Class and Motor Cycle Class inherit from Vehicle Class. While Truck, Sports Car, and S.U.V. extend Car Class. This diagram demonstrates the relations between our subclass and superclass.

Abstract Classes

Unlike an ordinary class, abstract classes can't be instantiated. This means that you can not create new instances of an abstract class, as you would an ordinary class. While you can't instantiate an abstract class, it can still share properties or be related to the subclass.

Creating an Abstract Class

In order to create an abstract class you must add the keyword: abstract to the class declaration.

Here are a couple of examples of abstract classes in Java:

// Here is our Vehicle Class
public abstract class VehicleClass
{

}

// Here is an example of our Shape class
public abstract class Shape
{

}

Creating Abstract Methods

Abstract methods are methods that are declared within an abstract class, but lack implementation. In most cases you would have to implement the method within the subclass. This way you have more flexibility with subclasses that require different logic in the same method.

Here is an example of abstract methods using our Vehicle Class

public abstract class VehicleClass
{
    private String type;
    private String vehicleName;

    public VehicleClass(String vType, String vName)
    {
        type = vType;
        vehicleName = vName;
    }

    /* This will need to be abstract, since 
     * we will need to implement different formulas
     * depending on if the vehicle is electric or 
     * gas powered.
     */ 
    public abstract double getMileage();
}

/* As you can see, in both classes, we have `getMileage` implemented
 * with different formulas.
 */ 
public class Truck extends VehicleClass
{
    private double gasTankCapacity;
    private double milesPerTank;

    public Truck(double capacity, double miles)
    {
        gasTankCapacity = capacity;
        milesPerTank = miles;
    }

    public double getMileage()
    {
        return milesPerTank/gasTankCapacity;
    }
}

public class ElectricCar extends VehicleClass
{
    private double maxCharge;
    private double milesPerCharge;
    private double maxEnergyUsage;

    public ElectricCar(double charge, double maxEnergy, double milesCharge)
    {
        maxCharge = charge;
        maxEnergyUsage = maxEnergy;
        milesPerCharge = milesCharge;
    }

    public double getMileage()
    {
        return (maxCharge*milesPerCharge)/maxEnergyUsage;
    }
}
Vehicle Class