Advertisement

header ads

Java OOP Polymorphism - A simple tutorial



Polymorphism is the ability to perform certain task in many ways. In polymorphism, same method use again and again to perform different tasks. It can be also defined as the ability of a object to take on many forms. This java polymorphism tutorial provides a simple introduction to understand the basics of polymorphism in java. Consider following example to get a clear idea.

Polymorphism.java class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Human {

}

class Man extends Human {

}

class Woman extends Human {

}

public class Polymorphism {
 public static void main(String[] args) {

 }
}

In above code, we can identify that,

  • Woman is a Human
  • Man is a Human
  • Woman is a Woman (considering class object reference: i.e. Woman woman = new Woman)
  • Man is a Man (considering class object reference)

Therefore both Man and Woman classes have more than one IS-A relationship. So as an example Man has a form of Man itself and also has a form of Human. If any java object has more than one form, it can be polymorphic.

There are two types of polymorphism.

Method Overloading
Method overloading allows us to use more than one method with same name and different parameters.

Consider following Polymorphism.java class
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public class Polymorphism {
 void myMethod() {
  System.out.println("This is my method without arguments");
 }

 void myMethod(String myString) {
  System.out.println("This is my method with argument: " + myString);
 }

 public static void main(String[] args) {
  Polymorphism polymorphism = new Polymorphism();
  polymorphism.myMethod();
  polymorphism.myMethod("Hello World");
 }
}

Output:
This is my method without arguments
This is my method with argument: Hello World

Method Overriding
Method Overriding allows to inherit a method of parent class or super class in child or sub class. Here the arguments should be same. Also, static, private or final methods cannot be override.

Consider following Polymorphism.java class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ParentClass {
 void myMethod() {
  System.out.println("This is parent class");
 }
}

class ChildClass extends ParentClass {
 void myMethod() {
  System.out.println("This is child class");
 }
}

public class Polymorphism {

 public static void main(String[] args) {
  ParentClass parentClassReferenceObject = new ParentClass();
  ParentClass parentClassReferenceChildObject = new ChildClass();

  parentClassReferenceObject.myMethod();
  parentClassReferenceChildObject.myMethod();

 }
}

Output:
This is parent class
This is child class

As you can see in line 17 ParentClass has refers the ChildClass object. If we add arguments to the myMethod of ChildClass, we are not able to call that method.

That was a quick tutorial to understand the basic idea of polymorphism. Now you can try more complex codes on your own. Like our facebook page and share this among friends. See you in next tutorial.


Post a Comment

0 Comments