programming, technology

How to think in objects and classes – Java — Part 4


Hello, welcome! Hope you’re enjoying this so far 😀 Anything you’d like me to clarify? alter? let me have your comments. Today’s topic is Polymorphism.

Polymorphism is also known as method overloading. It may look like a very difficult concept judging from its name but it is quite simple. It is simply the ability to call the same method in a variety of forms. That is, calling the same method with different number of arguments etc. Let’s have an analogy.

A person can talk but at the moment (from our Person class) a person can only say what we ask them to say (parse as argument). What if we want them to say a default thing when there’s no argument? Or answer a question when we make them know this is a question and not something they should say? That means we’re gonna have to overload the function talk().

Let’s take a look at our current talk() method.


public void talk(String what_to_say){
System.out.println(first_name+" says: "+what_to_say);
}

To overload this function, we simply create another one with the same name and then give it a different number of arguments, different return type or different types of arguments.


public void talk(String what_to_say){
System.out.println(first_name+" says: "+what_to_say);
}

public void talk(){
System.out.println("Hello there!");
}

With this method added, you can now call the talk() method with no arguments and it’ll work fine. Let’s add another one and make our talking more dynamic.


public void talk(String what_to_say,boolean question){
if (question){
System.out.println("Umm... I don't know");
}
else {
System.out.println("*Why call this function then (to myself)...*\n"+what_to_say);
}
}

Constructor overloading

Constructors can also be overloaded just like other methods. Due to the nature of constructors, overloading them makes it easy to declare objects in diverse ways. Let’s say you want to create an object without parsing any arguments. Let’s use our Person class to create a baby which has everything set to default.


//... add this new constructor

public Person(){
first_name = "";
surname = "";
location = "Hospital";
age = 0;
}


//... and this is can be called

Person baby = new Person();

That’s what Polymorphism is about. Hope it’s been demystified enough and I hope you enjoyed it. Take a look at Part 1 through Part 3 if you haven’t read them. Our next lesson will be our last for this series. We’ll talk about the misc stuff. Class variables, constants, class methods etc. Leave your comments, they’re needed.

Advertisements

1 thought on “How to think in objects and classes – Java — Part 4”

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