Polymorphism in Java

A while back a friend and beginning programmer asked me to explain “polymorphism” and why it’s important. It took me a few moments to figure out how to explain the concept and how to apply it. Here is what I came up with:

Polymorphism is the ability to abstract a class and get more granular with objects as you get into sub-classes. While that statement probably doesn’t clarify anything, i will expand on it until it should.

 

As applications grow in size and complexity, it gets difficult to keep everything in a parent class. These classes can get too large and difficult to manage. By breaking them into smaller classes that inherit the base class properties and methods you can safely break things down without losing functionality.

 

Take the concept of fruit. You currently have a class of food called fruit. Now obviously inside of fruit, you have different types. You have citrus (oranges and lemons), berries (blackberries, blueberries, and strawberries), melons (watermelons), and accessory fruits (apples). All of these types of fruits will inherit the base properties and methods of fruits. Each one will add it’s own properties and methods, but will contain the base information. Things like “does it have seeds” will be answered all the way down to the lowest level.

 

Now, since each fruit inside of each type is different, they will inherit the type of fruit, which will give them the base properties and methods of fruit and type. We will work with the citrus fruits in this concept. We have identified that citrus oranges and lemons fall in the citrus fruit category, so we will look at both of them. First we will setup our main class called Fruits.

 

public class Fruits {

public static boolean hasSeeds;

public static double weight;

public static String Name;

public static String Color;

}

 

Now that we have our main class with some generic properties, we can move into the first subclass. The main thing to remember here is to “extend” the Fruits class.

 

public class CitrusFruits extends Fruits {

public static boolean isAcidic;

public static double acidLevel;

public static double sugarLevel;

}

 

Now, we can create our Orange object. The Orange will inherit all of the properties of the first two classes with the ability to create it’s own.

 

public class Orange extends CitrusFruits {

public static String orangeType;

public static String orangeColor;

public static boolean isSweet;

 

public Orange() {

//this is our default constructor and will be used in the creation of the orange object if no variables are passed.

}

 

public Orange(String OrangeType, String OrangeColor, boolean IsSweet) {

orangeType = OrangeType;

orangeColor = OrangeColor;

isSweet = IsSweet;

} //this is a constructor for when we do pass variables

 

public String GetName() {

return Name; //this will return the name that is set in the Fruits class

}

 

public void SetName(String name) {

Name = name; //this will set the name in the fruits class

}

 

 

Now that we have our Orange class you can see how manipulating and getting information with the Fruits class is easy. It’s almost as if the variables were in the Orange class.

 

I have not only given a brief description of the concepts of polymorphism but also given a simple example on to utilize it and why it is important.