Understanding Java Polymorphism: The Easiest Way

Classmate of mine–is not actually a classmate, but can be said classmate because at that time I enter the class that not belong to my (even though the same Java class) and one of them did–said: “This class should not be called Java 2, should be Java 10, or Java 100”. All laugh.

Yeah, no one can deny that the logic in polymorphism was not easy at the first time. Even the most advanced programmer had time to imagine what the hell was that. Come’on, don’t said you understand Pythagoras, Algebra, chemical equilibrium since you were 5th grader. Gosh!

Okay okay. Then how we can learn it the easiest way?

Actually learning object oriented programming, in my point of view, is easy IF we know what object supposed to mean. Object is something that encapsulates attribute and operation. In its easiest term, object is what know something and know how to do something. Pen is an object, it knows its color, it knows how to write. Your keyboard is object, it knows what button is pressed by the user and it knows what value should be there in output if for example user press key ‘a’.

Haiyooo~ if about that, I know larrh… give me more give me more

Okay okay. This is getting interesting, why not you subscribe first FOR free?

Polymorphism directs its meaning directly to: programming in general. For the sake of this general, the class even have method that doesn’t specialize on what to do, what to behave. If you ask why we create that kind of class, it because we want to let the program to expand easily, meaning that, we cut down money when the time comes for us to CHANGE the program.

No program can survive without changing.

So how?

Imagine this. There are many animals in this world. There are mammals, fungi, etc. etc. In mammals, something like platypus, human, bat, are exist. Take example, we have fish and bird that is a type of animal. All animal knows its own position (point) where they stands. So we do have:

import java.awt.Point;
	private String animalName = "";
	private Point coordinat = new Point(0,0);

	public Animal(String animalName, int x, int y) {
		this.animalName = animalName;
		coordinat.setLocation(x, y);
	}

	public void setPosition(int x, int y) {
		this.coordinat.setLocation(x, y);
	}

	public String getAnimalType() {
		return animalName;
	}

That snippets tell that all animals knows what animal are they and where are they stand right now. How how how… how if… we want the animal to move? Okay, this is a little bit tricky.

All animal moves. Is it? Yeah, because they are living thing. Remember that all living thing are moving, eating, going to reproduction in a time in their life, and all all those biological research result done by someone out there the animal should know how to do.

But not animal doing, let say, moving in the same way. Bird is not swimming as fish even though both of them is doing ‘move’. Tiger eat differently than butterfly even though they both ‘eat’. How we accomplish that? polymorphism. We program in general. We don’t know how they eat. We do know they eat.

So, the whole class will be like this:

import java.awt.Point;

public abstract class Animal {
	private String animalName = "";
	private Point coordinat = new Point(0,0);

	public Animal(String animalName, int x, int y) {
		this.animalName = animalName;
		coordinat.setLocation(x, y);
	}

	public void setPosition(int x, int y) {
		this.coordinat.setLocation(x, y);
	}

	public String getAnimalType() {
		return animalName;
	}

	public abstract void move(int x, int y);
}

The class is abstract in the sense that no object can instantiate this class. This is abstract class. And we also have move() as abstract method as we don’t exactly know the detail of how animal is moving.

Now create two classes: Bird and Fish.

public class Bird extends Animal {

	public Bird(int x, int y) {
		super("Bird", x, y);
	}

	public void move(int x, int y) {
		super.setPosition(x, y);
		System.out.println("I am " +
				super.getAnimalType() +
				" and flying to x: " + x +
				" and y: " + y);
	}
}

And the fish:


public class Fish extends Animal {

	public Fish(int x, int y) {
		super("Fish", x, y);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void move(int x, int y) {
		super.setPosition(x, y);
		System.out.println("I am " +
				super.getAnimalType() +
				" and surfing to x: " + x +
				" and y: " + y);
	}

}

Now let’s write the tester code:


public class PolymorphismTest {
	public static void main(String[] args) {
		Animal theAnimal[] = new Animal[2];
		theAnimal[0] = new Bird(0,0);
		theAnimal[1] = new Fish(0,0);

		for (int i=0; i<=6; i++) {
			theAnimal[0].move(i, i+5);
			theAnimal[1].move(i, i+3);
		}

	}
}

And run it. You will got output:

I am Bird and flying to x: 0 and y: 5
I am Bird and flying to x: 0 and y: 5
I am Fish and surfing to x: 0 and y: 3
I am Bird and flying to x: 1 and y: 6
I am Fish and surfing to x: 1 and y: 4
I am Bird and flying to x: 2 and y: 7
I am Fish and surfing to x: 2 and y: 5
I am Bird and flying to x: 3 and y: 8
I am Fish and surfing to x: 3 and y: 6
I am Bird and flying to x: 4 and y: 9
I am Fish and surfing to x: 4 and y: 7
I am Bird and flying to x: 5 and y: 10
I am Fish and surfing to x: 5 and y: 8
I am Bird and flying to x: 6 and y: 11
I am Fish and surfing to x: 6 and y: 9

Fantastic rite. We don’t know how each animal is moving, so that we abstract it by meaning we know that all animal can move but we don’t exactly know how they move. See… how we program in general. When we talk about class that has abstract method in it, whatever class extends this class will HAVE TO implement (or override) this abstract class.

But… daradam… this design is actually a little bit flawing. Question is, let’s forget biology, how if some class of animal can’t move at all. Do we need to:

public void move(int x, int y) {
//nothing, I can't move
}

do something like that?

Answer is no. We simply not to do that. We will talk about this later, in ‘interface’ class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s