Previous lesson 3/5 |home| Next lesson 5/5

Object-Oriented Programming Concept

Is-a Relationship

We briefly mentioned earlier that the "is-a" relationship describes inheritance relationship between objects. If you can talk something with word "is". They can be described with keyword extends in coding. "is-a" relationship is also called classification.

Everything is an object. We can say that Pizza is an object. Pizza Hut creates a new kind of Pizza called Pepperoni. We can say Pepperoni is a Pizza. Papa John's got a hint, creating a RollsRoll. RollsRoll is a Pizza. Don't use such artificial names to order Pizza from these famous stores. You see, we use normal language to describe things with is. Let's translate them to Java code.

 
public class Pepperoni extends Pizza {//is-a relationship
    private int size;//has a relationship
    private String style;
    
    public int getSize() {
        return size;
    }
    public void setSize(int sz) {
       this.size = sz;
    }
    
    public String getStyle() {
       return style;
    }
    public void setStyle(String style) {
       this.style = style;
    }
}

Note that is-a relationship described with Java code by using extends keyword. Do you notice that the Pepperoni class doesn't have a topping and a sauce as its member fields? Ask yourself why? Because the super class Pizza already defines these two fields and the sub-class Pepperoni doesn't need to do that. The Pepperoni and Pizza classes are in the same family. This is so-called inheritance relationship. The getter/setter methods designed here to make the fields accessible.

For how to use these objects, please see another tutorials. Next lesson, we will give you more examples about how to use OO concept to code.

Previous lesson 3/5 |home| Next lesson 5/5