Class Design and Abstract Classes
Last updated
Last updated
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.
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.
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.
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:
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