Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Static Polymorphism in Java

In this article we will learn about static polymorphism and how it is implemented in Java.

Polymorphism is an important feature of object oriented programming and can be considered as one of the most important pillar of object oriented programming .

We have divided our tutorial into three parts mainly:

  • First part explains the meaning of polymorphism briefly
  • Second part explains static polymorphism and how it can be achieved
  • Third part explains a program written in Java that implements the static polymorphism.

Polymorphism

Before going in any sort of detail, let’s look at the word which is one of the import words of today’s tutorial polymorphism. It can be broken up into two pieces as described below.

  • Poly=many
  • Morphism=Forms/shapes

Polymorphism is one of the important fundamental features of object oriented paradigm. Literally, polymorphism means one name and many forms. In computer science we refer polymorphism as a capability of a method to have multiple definitions.

If you have ever played a famous game, Named as “Need for Speed” or any other game, where just with one click on start all the game gets started and the other cars start moving. All the function starts their functionality, this we call polymorphism with one call we can run many methods.

It allows you to define multiple methods with the same name but various definitions.

In Object oriented programming there are two types of polymorphism depending upon how method call is resolved:

  • Static polymorphism
  • Dynamic polymorphism

In this article we will be discussing static polymorphism and its implementation.

Static polymorphism

What is static polymorphism?

Static polymorphism is also called as compile time polymorphism. As we mentioned earlier in polymorphism there are multiple definitions of a single method in a program which means that we make multiple methods in a program with same name which may perform different task but provides the user or the programmer the facility and better readability of code. In static polymorphism, if we call such a method in our code then which definition of that method is to be called actually is resolved at compile time only.

We can define it as “In static polymorphism the actual method call is resolved statically that is at compile time only. “

Static polymorphism is achieved by method overloading in OOP languages such as in C++ and Java. In next section we define what is method overloading

Method overloading

Method overloading is a technique to implement static polymorphism in OOP languages in C++ and Java. Method overloading means to define two or more methods in a class with same name but different arguments. It means method overloading is a way to have two or more than two methods in a class with same name but different arguments. Here arguments can be different in number, data types or both number and data types.

Remember that if two methods have different return types only then they are not overloaded methods. The return type of methods does not have any role in method overloading. It is only the arguments passed that have a role in method overloading.

Implementation of Static polymorphism using method overloading

In this section we discuss an example of Java to explain the method overloading.

Example: Suppose we want to have methods in class to add the value of variables passed to the method as arguments. The variables can be two ints, three ints, two strings, three strings etc.

Let’s take a case where we need four methods which have arguments passed as:

  • A method that add two int like int, int
  • A method that add three int like int, int, int
  • A method that concatenate two string like String, String
  • A method that concatenate three string like String, String, String

One way to do this is to implement traditionally i.e. todefinefour methods with different names without the use of method overloading.

Listing 1: testing.java


Class Test {
	intaddtwoint (int a, int b) {
		returna+b;
}

	intaddthreeint (int a, int b, int c) {
		returna+b+c;
}


	Intaddtwostring (String a, String b) {
		returna+b;
}


	intaddthreestring (String a, String b, String c) {
		returna+b+c;
}
}

Now, to call these methods you have to remember the method name depending upon the argument you wish to pass before calling the actual methods as shown in code below:

Listing 2: Demo.java

class Demo {
	public static void main (String args []) {
		int result = addtwoint(10,10);
System.out.println(“result of method addtwoint  ”+result);
		result  =addthreeint(10,10,20);
System.out.println(“result of method addthreeint  ”+result);

		result  =addtwostring(“firstname”, “lastname”);
System.out.println(“result of method addtwoString  ”+result);

		result  =addthreestring(“firstname”, “ ”, “lastname”);
System.out.println(“result of method addthreestring  ”+result);
	
}
}

So in the above case, you need to remember the method names individually for each type that you have created. Which becomes very tedious when we have many methods in a program, to overcome this problem and get better readability of the code we make use of the method overloading.

You may have a question, What happen if we have multiple methods with same arguments?

In this scenario, ambiguity occurs that the compiler gets confused which method to call, and will generate error. It notifies the user to remove the error by either overloading the methods properly or removing the methods with same name.

Now see how we can have the same implemented using method overloading. You need to do the following:

  • You can have multiple methods with same name in a class
  • Remember to have the arguments passed to the methods should be different
    • Either in number like you can have a method with two int and a method with same name with three int. Here the data type is same but number of arguments passed is different. In first case it is two int type argument method and second is three int type argument method.
    • Below are two overloaded methods where the argument number is the overloading criteria:

      int  add(int a, int b) {}
      int add(int a, int b, int c) {}
    • Arguments are different in type. That means you can have a method with two int and another method with same name with two string type arguments.

    As shown below, two methods with the same name add but with different arguments type. First is accepting two int type arguments and second is accepting two String type arguments.

    int add(int a, int b) {}
    int add(String a, String b) {}

So our above problem can now be solved using method overloading as shown in program below.

Listing 3: Test.java

class Test {

// METHOD I which takes two arguments of type int
intadd (int a, int b) {
		returna+b;
}

// METHOD II which takes three arguments of type int

	intadd (int a, int b, int c) {
		returna+b+c;
}

	// METHOD III
	intadd (String a, String b) {
		returna+b;
}

	// METHOD IV
	intadd (String a, String b, String c) {
		returna+b+c;
}
}

Here you have all the four methods with the same name i.e. add but the argument type or argument number is different in each defined method. All the methods are overloaded. So we do not need to do that tedious work of remembering the names of the methods which use in our program.

Now we can call these methods with a single name add.

Listing 4: Demo.java

class Demo {
	public static void main (String args []) {
	int result =  add(10,10);  // calls the METHOD I
System.out.println(“result of method addtwoint  ”+result);
		result  =addthreeint(10,10,20); // calls the METHOD II
  	System.out.println(“result of method addthreeint  ”+result);
	result  =addtwostring(“firstname”, “lastname”); // calls the METHOD III
System.out.println(“result of method addtwoString  ”+result);
	result  =addthreestring(“firstname”, “ ”, “lastname”); // calls the METHOD IV
System.out.println(“result of method addthreestring  ”+result);
		}
}

Now one thing to understand here is how it gets resolved which method to call actually. The method to be called actually is on the basis of actual value passed to the method at the time of calling. It is resolved by compiler at the compile time only. The compiler resolves the method call depending on the actual arguments passed.

Like in above case the call resolves to calling the METHOD I defined in class Test because the arguments passed are 10, 10 i.e int, int hence the METHOD I is resolved by compiler.

int result =  add(10,10);  // calls the METHOD I

Similarly calling like resolves into calling the METHOD IV because the arguments passed are three and all of them are String type. So the compiler has no confusion in calling the right method.

result  = add (“firstname”, “ ”, “lastname”)// calls the METHOD IV

Conclusion:

This is how the static polymorphism can be implemented using method overloading technique in java. Now as you have learned the concept of polymorphism and static polymorphism, you can think of implementing it in any of your project so that you get hold on this concept further. Since it is one of the most important features of object oriented programming, so understanding of polymorphism is having great value.

Happy Reading!

See also



computer science Graduate :Ceo at www.gigsclerk.com (Best Marketplace to Buy and sell service online from 2$-100$ ) and www.trustingeeks.com. Interested in programming, online buissiness. Android development.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login