Back To Class
I will be going back to classes again after taking a short break from it. I will be continuing from the previous lesson on classes so pls do refer to it if you are lost or you have not read it before. The post can be found here. Today I will be writing more on the relationships between the classes and how it can be used to take your programming into another level. Besides calling it class and subclass , the relationship also often called parent and child class , just a FYI in case you are wondering what is the different between those two names.
Class Examples
Lets go back to the Bicycle class we looked at previously and then we look into a subclass of it.
public class Bicycle {
// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;
// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}
// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}
public void setGear(int newValue) {
gear = newValue;
}
public void applyBrake(int decrement) {
speed -= decrement;
}
public void speedUp(int increment) {
speed += increment;
}
}
And the subclass or the child class ,
public class MountainBike extends Bicycle {
// the MountainBike subclass has one field
public int seatHeight;
// the MountainBike subclass has one constructor
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}
// the MountainBike subclass has one method
public void setHeight(int newValue) {
seatHeight = newValue;
}
}
In order to fully understand the class relationships , we must understand one of the main OOP concepts , called “inheritance”. Of course we all know what is the meaning of the word inheritance but we must now look at it from the view of an OOP programmer. Suppose there is a father and a child , in our imaginary land , flying kite. The father is sitting beneath a pine tree and the child is running with the kite. What can we say about the relationship? Here are a few things ,
- Both have similar physical and non-physical attributes , such as arms , legs , eyes , names , address , etc ..
- Both have similar similar capabilities , such as reading , running , talking , laughing and so on
- But the child has a kite while the father does not have any
So for those which are similar between the father and child , we can say that the child has inherited from the father. Of course father is a human so we can say all human belong to a one distinct class called a “person” class. But for attributes or capabilities , functions , that the child has but the father doesn’t , maybe playing computer games , then we can associate it with the child and child alone.
If you were to examine closely at the MountainBike Class which “extends” or inherits from the Bicycle class ,it has a new attribute and a new method associated with the attribute, which are not available to its parent class. Therefore , if you are to create a new bicycle , you could never set its seatHeight to any value because it doesn’t exist for a normal Bicycle. Only a MountainBike would have the ability to adjust its seatHeight.
And if you look at the constructor of the MountainBike , you would see the word “super” before “startCadence” , “startSpeed” and “startGear” , which is saying that those are the same the parents. The only new value set would be the “startHeight”.
Class Relationships Conclusion
I hope you have a much clearer idea of class relationships and inheritance after reading. Hope to see you again!..