Polymorphism

Polymorphism

Basically, 'Poly' means many and 'Morph' means behavior or forms so many behaviors in programming. same functionality are same object which has many behaviors.



Example Code for Ploymorphism Concept

class DemoOverload{

    public int add(int x, int y){  //method 1

    return x+y;

    }

    public int add(int x, int y, int z){ //method 2

    return x+y+z;

    }

    public int add(double x, int y){ //method 3

    return (int)x+y;

    }

    public int add(int x, double y){ //method 4

    return x+(int)y;

    }

}

class Test{

    public static void main(String[] args){

    DemoOverload demo=new DemoOverload();

    System.out.println(demo.add(2,3));     //method 1 called

    System.out.println(demo.add(2,3,4));   //method 2 called

    System.out.println(demo.add(2,3.4));   //method 4 called

    System.out.println(demo.add(2.5,3));   //method 3 called

    }

}



There are two common ways of implementing Polymorphism.
1. Overloading
2. Overriding

Overloading
Using the same method name with different parameter types lists.

Overriding
Using the different implementations of the same method in sub classes.

Abstraction

Abstraction

Normally abstract means eliminate, here show only the necessary details and hide other details. abstract also is a keyword in java.
Eg:- In this picture, we can access the iPhone but we didn't know about the inside functions , circuit  and other things. like wise in the abstract concept it's show the appropriate details and hide the inappropriate details.



Example code for the Abstraction Code

    abstract class MobilePhone
    {
        public void Calling();
        public void SendSMS();
    }

    public class Nokia1400 : MobilePhone
    {

    }

    public class Nokia2700 : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
    }

    public class BlackBerry : MobilePhone
    {
        public void FMRadio();
        public void MP3();
        public void Camera();
        public void Recording();
        public void ReadAndSendEmails();

    }




Encapsulation

Encapsulation

 This one of the basic concept of OOP. this is approach to make security to the class, such as private or public. so in this encapsulation it can hide the fields in the class..




Example code for the Encapsulation Concept

class Demo
{
   private int _mark;

   public int Mark
   {
     get { return _mark; }
     set { if (_mark > 0) _mark = valueelse _mark = 0; }
   }
 }