Classes & Subclasses | Class Relationships

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 ,

  1. Both have similar physical and non-physical attributes , such as arms , legs , eyes , names , address , etc ..
  2. Both have similar similar capabilities , such as reading , running , talking , laughing and so on
  3. 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!..

SQL index options

Here is a brief look at some of the common options for SQL table index.

 create table customer (
            id integer not null auto_increment primary key ,
            //other column headers here ,
            (index)name
); 

What can we say about this insert statement? Here are a few things that are top of my head ,

  • It has two index , id and name.
  • id , has following properties , it is an integer , it cannot be null , and it is increased itself automatically.

Well then you might ask what is an SQL index and what does it do? Basically , it functions very much similar to indexes in libraries. It allows locating information with the database much faster just like the books arranged by alphabetical indexes in libraries. If the library is well indexed by alphabet , there is no need to search the whole library for a book , instead you would only need to know the first letter of the book and then go to the section containing it. It makes the process much faster than searching every rows for it.

Pls keep in mind that when you design a database , its best to keep the index numbering away from the information and within your control. What do I mean is that suppose you are designing a table for books , with author’s information and date published and so on. What would the index for such a table be? It is tempted to say ISBN but what if ISBN changes? It has , from 10 to 13 digits. You can’t control it and you may have no choice but to modify your whole database due to change in index from a table. So instead of ISBN , you might simple put 1 , 2 , 3 4 and so on as the index for the book table. It means that whatever the changes to the ISBN in future , there is no need to make much changes since every SQL , every PHP or Java codes will be referencing the internal index numbers and not ISBN.

Hope it clears up indexes in SQL databases as much as I have myself. See ya next time!