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

Bubble sort Implementation

Bubble sort  : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). // Bubble Sort class BubbleSort { public static void sort ( int [] array) { int n = array. length ; while ( true ) { boolean swapped = false; for ( int i = 0 ; i < n - 1 ; i++) { if (array[i + 1 ] < array[i]) { swap (array , i , i + 1 ) ; swapped = true; } } if (!swapped) break; } } private static void swap ( int [] array , int i , int j) { int temp = array[i] ;...

Object-Oriented Programming Concept in Java

OOPS( Object-Oriented Programming ) Concept in Java :   As we all know Java is Object Oriented programming language and what exactly it means in simple words to understand can be described as whatever is going to happen by Java , it will be based on some Object.  So next question can be what is Object ? , "Object is the representation or reference of Class to access its properties and use its behaviour ", now next is What is Class in java and answer to this question is "A class in java is the blueprint of Properties and Behaviours of it's own Object" as explained in my previous post  BASIC OVERVIEW OF JAVA  (SESSION 1)   Let's understand through an example : public class FirstJavaProgram { int firstNumber=10; int secondNumber=20;      public int sum(int fNum, int sNum){         return fNum+sNum;     }     public static void main(String[] args) {     //our logics ...

Overview of time and space complexity for sorting algorithm

Bubble sort : In bubble sort ,we will select the 1st element and compare with all the remaining element, same process we will continue for all the elements as we are traveling the whole Array 2 times except the element which we have selected to compare with other elements but still it will be consider as n time.    So time complexity for bubble sort will be O(n^2).         space complexity for bubble sort will be O(1). Selection sort : In selection sort we will divide the Array in 2 parts , sorted and unsorted and select the min element from unsorted and will copy to sorted array.      Time Complexity for selection sort also is O(n^2).      space complexity for bubble sort will be O(1). Insertion sort :  In insertion sort, Same process like selection sort , we will divide the array in sorted and unsorted array and we will select element from unsorted array and will insert it into sorted array at its proper...