Concepts of OOP..


Wednesday, December 10, 2014

Types of polymorphism

Polymorphism types

 

  Polymorphism is the ability of an object to take on many forms. It can happen when a method or a constructor is behaving differently in a class. There are 3 categories of polymorphism:
 

1. Method overloading

2. Method overriding

3. Constructor overloading


An operation can exhibit different behavior in different instances.This behavior depends on the types of data used in the operation.


Method Overloading happens when two or more methods of same name is defined in a class, provided that their argument list or parameters are different.

In Method Overriding, the Child class has the same method as of base class. In such cases child class overrides the parent class method without even touching the source code of the base class

Like method overloading,constructors can also be overloaded.Constructor overloading is way of having more than one constructor which does 2 different tasks.


METHOD: A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method’s name.

 CONSTRUCTOR: Constructor is a block of code, which runs when you use new keyword in order to instantiate an object. It looks like a method but the difference is Methods have return type but constructors don’t have any return type. And constructors must have the same name as the class name which does not require for methods.


Examples:

 

Method overloading:

class Overload
{
    void demo (int a)
    {
       System.out.println ("a: " + a);
    }
    void demo (int a, int b)
    {
       System.out.println ("a and b: " + a + "," + b);
    }
    double demo(double a) {
       System.out.println("double a: " + a);
       return a*a;
    }
}
class MethodOverloading
{
    public static void main (String args [])
    {
        Overload Obj = new Overload();
        double result;
        Obj .demo(10);
        Obj .demo(10, 20);
        result = Obj .demo(5.5);
        System.out.println("O/P : " + result);
    }
}
Here the method demo() is overloaded 3 times: first having 1 int parameter, second one has 2 int parameters and third one is having double arg. The method which is invoked/called depends on the type and number of parameters used for the input.



Method overriding:


public class BaseClass
{
    public void methodToOverride() //Base class method
    {
         System.out.println ("I'm the method of BaseClass");
    }
}
public class DerivedClass extends BaseClass
{
    public void methodToOverride() //Derived Class method
    {
         System.out.println ("I'm the method of DerivedClass");
    }
}

public class TestMethod
{
     public static void main (String args []) {
        // BaseClass reference and object
        BaseClass obj1 = new BaseClass(); 
        // BaseClass reference but DerivedClass object
        BaseClass obj2 = new DerivedClass(); 
        // Calls the method from BaseClass class
        obj1.methodToOverride(); 
        //Calls the method from DerivedClass class
        obj2.methodToOverride(); 
     }
}


Constructor overloading:


package beginnersbook.com;
public class StudentData
{
   private int stuID;
   private String stuName;
   private int stuAge;
   StudentData()
   {
       //Default constructor
       stuID = 100;
       stuName = "New Student";
       stuAge = 18;
   }
   StudentData(int num1, String str, int num2)
   {
       //Parameterized constructor
       stuID = num1;
       stuName = str;
       stuAge = num2;
   }
   //Getter and setter methods
   public int getStuID() {
       return stuID;
   }
   public void setStuID(int stuID) {
       this.stuID = stuID;
   }
   public String getStuName() {
       return stuName;
   }
   public void setStuName(String stuName) {
       this.stuName = stuName;
   }
   public int getStuAge() {
       return stuAge;
   }
   public void setStuAge(int stuAge) {
       this.stuAge = stuAge;
   }
}

class TestOverloading
{
   public static void main(String args[])
   {
       //This object creation would call the default constructor
       StudentData myobj = new StudentData();
       System.out.println("Student Name is: "+myobj.getStuName());
       System.out.println("Student Age is: "+myobj.getStuAge());
       System.out.println("Student ID is: "+myobj.getStuID());

       /*This object creation would call the parameterized
        * constructor StudentData(int, String, int)*/
       StudentData myobj2 = new StudentData(555, "Chaitanya", 25);
       System.out.println("Student Name is: "+myobj2.getStuName());
       System.out.println("Student Age is: "+myobj2.getStuAge());
       System.out.println("Student ID is: "+myobj2.getStuID()); 
  }
}
 
As you can see above, while creating the instance myobj
default constructor (StudentData()) gets called. But during 
the creating of myobj2, the arg-constructor 
(StudentDate(int, String, int))was called. Since both 
the constructors are having different initialization code, 
the variables value are different in each case.
 


 (These examples were taken from http://beginnersbook.com)

1 comment: