Polymorphism

The ability of an object to behave differently in different forms is called as polymorphism. So the methods have multiple implementations and the compiler or in runtime java will decide which method to execute. If the compiler can decide it, we called it static polymorphism (example: method overloading) while if it is determined in runtime we called it dynamic polymorphism (example: method overriding). Below code is an example for method overriding.


Code:

public class testPolymorphism{
public static void main(String []args){
Person p1;
p1= new Person("John",20);
p1.eat();
p1.sleep();
p1= new Student("Merry",16);
p1.eat();
p1.sleep();
}
}

class Person{
String name; int age;
Person(String name,int age){
this.name=name;
this.age=age;
System.out.println(this.name+":"+"I'm a Person");
} void eat(){
System.out.println(this.name+":"+"Eating...");
}
void sleep(){
System.out.println(this.name+":"+"Sleeping 8 hours...");
}
}

class Student extends Person{
Student(String name,int age){
super(name,age);
System.out.println(this.name+":"+"I'm a Student");
}
void sleep(){
System.out.println(this.name+":"+"Sleeping 6 hours...");
}
void study(){
System.out.println(this.name+":"+"Studying...");
}
}


Output:
John:I'm a Person John:Eating...
John:Sleeping 8 hours...
Merry:I'm a Person
Merry:I'm a Student
Merry:Eating...
Merry:Sleeping 6 hours...


Code Explanation:

public class testPolymorphism{
public static void main(String []args){
Person p1; // Declaring an object called p1 with the data type of Person 
p1= new Person("John",20); // Assigning a new Person Object (p1 is pointing to a Person Object) 
p1.eat(); //Calling eat method implemented in Person class 
p1.sleep(); //Calling sleep method implemented in Person class 
p1= new Student("Merry",16); //Assigning a new Student object to p1 (p1 now pointing to a Student Object but the data type is still Person.) 
p1.eat(); //Calling eat method implemented in Person class(Student inherits every method in Person class) 
p1.sleep(); /*Calling method Implemented in Student class (sleep method in Person class has overridden by sleep method in Student class)*/
}
}

class Person{ //super class
String name;
int age;
Person(String name,int age){ //Constructor
this.name=name;
this.age=age;
System.out.println(this.name+":"+"I'm a Person");
} void eat(){
System.out.println(this.name+":"+"Eating...");
}
void sleep(){
System.out.println(this.name+":"+"Sleeping 8 hours...");
}
}

class Student extends Person{ //Sub class
Student(String name,int age){ //Constructor
super(name,age);
System.out.println(this.name+":"+"I'm a Student");
}
       void sleep(){ //Overridden the sleep method inherited from Person class (sub class implementing the method of its super class again)
System.out.println(this.name+":"+"Sleeping 6 hours...");
}
void study(){
System.out.println(this.name+":"+"Studying...");
}
}


Another way that we can implement the concept of polymorphism in java is method overloading. There can be methods with same name but with different parameters (this is possible because Java identify it methods by the method signature (method name+parameter list) ) parameter list can be differ in three ways
  1. number of parameters
  2. data type of the parameters
  3. order of parameters
So if one of the above three is different method signature will change.


Code:

 public class testPolymorphism{
public static void main(String []args){
Type t = new Type();
t.printMe(12);
t.printMe(6.13);
t.printMe(85.2f);
t.printMe('a');
t.printMe("java");
t.printMe(12,12.6f);
t.printMe(12.6f,12);
}

class Type{
public void printMe(int a){
System.out.println("Integer");
}
public void printMe(int a, float b){
System.out.println("Integer and a float");
}
public void printMe(float a, int b){
System.out.println("float and an Integer");
}
public void printMe(char a){
System.out.println("Character");
}
public void printMe(double a){
System.out.println("Decimal");
}
public void printMe(String a){
System.out.println("String");
}
}


Output:
 Integer Decimal Decimal Character String Integer and a float float and an Integer


So in the above code same method is called but they behave differently. this is called as static polymorphism because in the compilation time compiler can determine which method to refer. in dynamic polymorphism, method which should refer is determined in the runtime according to the object which reference variable is pointing at.

Comments

Popular posts from this blog

Flow Charts

ධර්මය, ආක්‍රමණය හා යුද්ධය

ආගමක් නැති මනුස්සයෙක්