Skip to main content

OOPS Concept in Java : POLYMORPHISM

 POLYMORPHISM IN JAVA : 

Polymorphism means mutiple forms of Single reference. And to understand in simple way just take an example from my previous post of OOPS CONCEPT IN JAVA : INHERITANCE , I request you guys to go through this link before proceding here. So in this post I have created a method called sum() in PARENT class which has been used by CHILD class without writing same sum() method in CHILD class. This was possible becuase of INHERITANCE concept in java. 

But what if CHILD class is not satisfied with PARENT sum() and CHILD class wants to improve it like by adding some message before the calculation. To do this we have another OOPS CONCEPT IN JAVA i.e POLYMORPHISM and by applying this logic we can make same sum() method behvae differently based on Object. As I mentioned earlier POLYMORPHISM means different forms and same has been achieved here by calling same sum() method but the output is different based on Object on which it has been called. If Object is of PARENT type then PARENT's sum() method will be executed and if Object is of CHILD class type then CHILD's sum() method will be executed. This is called method overriding in which PARENT method has been overriden by CHILD class. 

package C4CAMPUS;

class PARENT2 {

int a = 10;
int b = 20;

public int sum(int fNumber, int sNumber) {

int result = fNumber + sNumber;
return result;

}
}

class CHILD2 extends PARENT2 {

int a = 60;
int b = 50;

public static void main(String[] args) {

PARENT2 parent =
new PARENT2();
CHILD2 child = new CHILD2();
System.out.println(parent.sum(parent.a, parent.b));
System.out.println(child.sum(child.a, child.b));


}

public int sum(int fNumber, int sNumber) {

int result = fNumber + sNumber + 10;
return result;

}

}


Output will be :

30 and 120 

Because for PARENT class it was just a sum of 10 and 20 which is equal to 30 but for child it was sum of 50 and 60 but we have added extra 10 in child sum() method that's why result is 120 not 110. So now as we obersved same sum() method is excuting to different kind of logic based on Object so this is an example of POLYMORPHISM which is called METHOD OVERRIDING deciding at runtime . Please note that method signature and input paremeter type and sequence should be same as PARENT while overriding if not then it will be consider as METHOD OVERLOADING which can be decieded by JVM at compile time only.

  Now let's discuss METHOD OVERLOADING . This is liitle same as METHOD OVERRIDING but instead of deciding which forms should be executed based on reference OBJECT , in METHOD OVERLOADING it will be decieded on the METHOD SIGNATURE which can be differ by INPUT PARAMETER,INPUT SEQUENCE,INPUT TYPE, INPUT  COUNT .What actually it means we will try to understand by an example : 

class CHILD2  {

public static void main(String[] args) {

CHILD2 child = new CHILD2();

System.out.println("NORMAL PARAMETER = "+child.sum(10,20));
System.out.println("WITH EXTRA INT PARAMETER = "+child.sum(10,20, 10));
System.out.println(child.sum(10,20,"WITH EXTRA STRING PARAMETER = "));
System.out.println(child.sum("WITH DIFFERENT STRING SEQUENCE = ",10,20));

}

public int sum(int fNumber, int sNumber) {

int result = fNumber + sNumber;
return result;

}
public int sum(int fNumber, int sNumber, int extra) {

int result = fNumber + sNumber + extra;
return result;

}

public int sum(int fNumber, int sNumber, String message) {

int result = fNumber + sNumber ;
System.out.print (message);
return result;

}
public String sum(String message, int fNumber, int sNumber) {
int result = fNumber + sNumber ;
return message+result;

}

}

Output will be as below :

NORMAL PARAMETER = 30

WITH EXTRA INT PARAMETER = 40

WITH EXTRA STRING PARAMETER = 30

WITH DIFFERENT STRING SEQUENCE = 30

So if you notice we are calling same sum() method but with different input PARAMETER and method is executing differently. This is called METHOD OVERLOADING.

Now you should have got clear idea about METHOD OVERRIDING and METHOD OVERLOADING. This is most asked interview question so you should not have any confusion in it, if you still find some difficulties to undersatand , please let me know in comment section.

One doubt you can have that in case of STATIC method how METHOD OVERRIDING will work Right ?. As STATIC means it belongs to CLASS not object as explained in my previous post BASIC OVERVIEW OF JAVA . 

 Answer to this question is very simple as STATIC means it belongs to CLASS so any method with same signature has been define in both the classes PARENT and CHILD then it won't override rather it will just hide the method of other class. Just see below example you will have clear idea.


class PARENT2{

public static int sum2(int fNumber, int sNumber) {

int result = fNumber + sNumber+ 100 ;
return result;

}
}


class CHILD2 {

public static void main(String[] args) {

System.out.println("PARENT SUM() = "+PARENT2.sum2(10, 20));
System.out.println("CHILD SUM() = " +CHILD2.sum2(10, 20));

}

public static int sum2(int fNumber, int sNumber) {

int result = fNumber + sNumber + 200;
return result;

}

}

Output will be :

PARENT SUM() = 130

CHILD SUM() = 230

This is all about POLYMORPHISM  realted to methods at RUNTIME and COMPILE time.

As mentioned in my previous post PARENT can be refernce of CHILD class object but CHILD class can not be refrence of PARENT class in my previous post OOPS CONCEPT IN JAVA : INHERITANCE . Same way you can think as an INTERFACE is the reference of all classes which are implementing the INTERFACE. And at the RUNTIME reference of INETERFACE will get change to any of the Object of classes which has implemented the INTERFACE. This is also an example polymorphism. Let's see this through an example 

interface I {

}

class A implements I{
A(){
System.out.println("OBJECT OF A");
}
}
class B implements I{
B(){
System.out.println("OBJECT OF B");
}
}
class C implements I{
C(){
System.out.println("OBJECT OF C");
}
}

class INTERFACEPLOY{

public static void main(String args []){
I a=new A();
I b=new B();
I c=new C();

}

}

Output will be : 

OBJECT OF A

OBJECT OF B

OBJECT OF C

As we can see in above example reference of all three Object is INTERFACE "I" only but it can hold the object of all the classes which are implementing it.

Please go through out previous OOPS CONCEPT POST also which can help you to understand the OOPS CONCEPT in simplest way.


OOPS CONCEPT :
  1. OOPS CONCEPTS IN JAVA
  2. INHERITANCE IN JAVA
  3. ABSTRACTION IN JAVA
  4. POLYMORPHISM IN JAVA
  5. ENCAPSULATION IN JAVA

Comments

Popular posts from this blog

Java Program : Writing First Java Factorial Program with explanation

 NAMING CONVENTION IN JAVA : Java is an object oriented programming language , we can relate it to real life object like i mapped Java with human in my previous post JAVA OVERVIEW (SESSION 1)  and represent human properties like body parts as properties in Java and Human can dance , drive , walk , run these can be mapped as Behaviour in java.    Now To represent properties and behaviour in java , there are some standard naming conventions we should follow. Class name should always starts with Uppercase letter like class Student { //Code to be executed } Properties or any kind of variables should starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ; //Code to be executed } Methods name should also starts from lower case and afterwards every first letter of each next word should be in Upper case . like class Student { int studentId ; String studentName ;

OOPS Concept in Java : ENCAPSULATION

OOPS Concept in Java : ENCAPSULATION   This OOPS concept can be used to make things simpler in case of software development . Main purpose of this concept is to hide the properties of any class and give access to fetch and modified based on business criteria.  A simple example can be a POJO ( Plain Old Java Object) in which all the properties of a class can be private and through getter and setter method of properties we can fetch and update the properties of Object. So instead of having direct access to properties we have created 2 methods to make the CLASS things encapsulated in single unit while access to it is via 2 public methods.   Just consider we have requirement that once the object is created its value should not be changed then simplest way to achieve this  can be done by just removing setter method and we will keep only getter methods to access Object properties . In this case after Object creation whatever the value of Object properties has been initialised it will b