# Class Design and Abstract Classes

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: ![Vehicle Class](/files/-M4CF6tzV9s5VvpdLK5v)

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:

```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***

```java
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;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codehs.gitbook.io/apjava/classes-and-object-oriented-programming/class-design-and-abstract-classes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
