Click here to Skip to main content
Click here to Skip to main content

METHOD Overload Vs Overriding.

, 15 Nov 2006
Rate this:
Please Sign up or sign in to vote.
Overload Vs Overriding.
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

Introduction

Q: Method Overloading ?

Ans:

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 anoter one in base class and another in derived class.

 

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(like User's first Name)

Person(userFirstName,userLastName); // as a constructor and call method with one parameter(like User's first Name)

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.

-----------------------------------------------------------------------------------------

Q: Method Overriding?

Ans:

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 methods

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.

Step 1

In this example we will define a base class called Circle

class Circle
{

//declaring the instance variable

protected double radius;

public Circle(double radius)
{
      this.radius = radius;
}

// other method definitions here

public double getArea()
{
      return Math.PI*radius*radius;

}//this method returns the area of the circle

}// end of class circle

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. The derived class will be the Cylinder class. The getArea() method in the Circle class computes the area of a circle, while the getArea method in the Cylinder class computes the surface area of a cylinder.

The Cylinder class is defined below.

class Cylinder extends Circle
{

//declaring the instance variable

protected double length;

public Cylinder(double radius, double length)
{
     super(radius);
     this.length = length;
}

// other method definitions here

public double getArea()
{
     // method overriden here
     return 2*super.getArea()+2*Math.PI*radius*length;
}//this method returns the cylinder surface area

}// end of class Cylinder

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).

 

Example code

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);

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

PavanPareta
Software Developer (Senior) WmDev Technology
India India
Pavan is a Senior software developer, and has been in the industry since August 2005. He has experience in Windows Phone 7, Windows Mobile with Compact Framework, Python for S60, C#.NET, VB.NET, Windows Forms, ASP.NET, WCF, Silverlight, JavaScript and HTML.
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 4 PinmemberShashwath72-Sep-12 19:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 15 Nov 2006
Article Copyright 2006 by PavanPareta
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid