Classes can have different relationships in java.
Programs utilize these for their code to run efficently.
The four relations of classes are subclasses, super classes, abstract classes and interfaces
Going to show the difference between a subclass and super with my Software Object Project
| SuperClass | SubClass |
|---|---|
| More Generic Form of a class. Called the parent class. Can store children instances in the parent object. Downcasting require to run child's non-overriding methods |
Inherits the attributes and methods (More specific functionality) and can override the parent class. Its constructor needs to run parent's constructor. |
| |
|
Copy
|
Copy
There is much less code for the sub-class compared to the parent class. By extending a class, you allow the child class to inherit the functionality of the parent. Meaning, you don't need to rewrite methods unless you are overriding them. |
Did you know that you can set a super class variable to an instance of a sub class. Like, so
| Abstract Class | Interfaces |
|---|---|
| Acts like a normal class, but has some undefined functions for methods called abstract methods. These are to be overriden by a subclass. If all abstract methods are not overriden by child, child must be abstract. | An abstract class, but it cannot define any functions. It also can't set any attributes. It is a list of methods needed for classes that implement the interface. If all methods are not implemented, class becomes abstract. |
Here is an abstract class room I made for the Fantasy Assignment Copy |
I didn't use any interfaces for projects, but here is one from a practice test Copy
Interfaces are practically checklists for functions and why it is only used if there are a lot of functions required for a program. Graphics program would use Interfaces as there is a decent amount of methods required. |