Thursday, November 29, 2012

Method Overloading and Overriding in Object Oriented Programming


Method Overloading:

Method overloading means having two or more methods with the same name but different signatures in the same scope. These two methods may exist in the same class or one in base class and another in derived class. 

E.g.
class Person
{
private String firstName;
private String lastName;
Person()
{
this.firstName = "";
this.lastName = "";
}
Person(String FirstName)
{
this.firstName = FirstName;
this.lastName = "";
}
Person(String FirstName, String LastName)
{
this.firstName = FirstName;
this.lastName = LastName;
}
}

Calling Overloaded Methods.
Person(); // as a constructor and call method without parameter
Person(userFirstName); // as a constructor and call method with one parameter
Person(userFirstName,userLastName); // as a constructor and call method with one parameter


When to use Method Overloading?
Generally, you should consider overloading a method when you have required same reason that take different signatures, but conceptually do the same thing. 

Method Overriding:
Method overriding means having a different implementation of the same method in the inherited class. These two methods would have the same signature, but different implementation. One of these would exist in the base class and another in the derived class. These cannot exist in the same class. 

Overriding method definitions
In a derived class, if you include a method definition that has the same name and exactly the same number and types of parameters as a method already defined in the base class, this new definition replaces the old definition of the method.

Explanation
A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to modify the methods defined in the superclass. This is referred to as method overriding. The following example demonstrates method overriding.
In this example we will define a base class called Circle
Step 1
class Circle
{
protected double radius;
public Circle(double radius)
{
      this.radius = radius;
}
public double getArea()
{
      return Math.PI*radius*radius;

}
}

When the getArea method is invoked from an instance of the Circle class, the method returns the area of the circle.
Step 2
The next step is to define a subclass to override the getArea() method in the Circle class.
class Cylinder extends Circle
{
protected double length;
public Cylinder(double radius, double length)
{
     super(radius);
     this.length = length;
}
public double getArea()
{
     // method overriden here
     return 2*super.getArea()+2*Math.PI*radius*length;
}

}

When the overriden method (getArea) is invoked for an object of the Cylinder class, the new definition of the method is called and not the old definition from the superclass (Circle).
This is the code to instantiate the above two classes

Circle myCircle;
myCircle = new Circle(1.20);
Cylinder myCylinder;
myCylinder = new Cylinder(1.20,2.50);

No comments:

Post a Comment