Tutorial 5 : Overloading

Method Overloading is a feature that allows a class to have two or more methods having same name, if their argument lists are different. In the last tutorial we discussed constructor overloading that allows a class to have more than one constructors having different argument lists.
Argument lists could differ in –
1. Number of parameters.
2. Data type of parameters.
3. Sequence of Data type of parameters.
Method overloading is also known as Static Polymorphism.
Points to Note:
1. Static Polymorphism is also known as compile time binding or early binding.
2. Static binding happens at compile time. Method overloading is an example of static binding where binding of method call to its definition happens at Compile time.

Problem Description:

How to overload methods ?


This example displays the way of overloading a method depending on type and number of parameters.
class MyClass {
   int height;
   MyClass() {
      height = 0;
   MyClass(int i) {
      System.out.println("Building new House that is "
      + i + " feet tall");
      height = i;
   void info() {
      System.out.println("House is " + height
      + " feet tall");
   void info(String s) {
      System.out.println(s + ": House is "
      + height + " feet tall");
public class MainClass {
   public static void main(String[] args) {
      MyClass t = new MyClass(0);
      t.info("overloaded method");
      //Overloaded constructor:
      new MyClass();


The above code sample will produce the following result.
Building new House that is 0 feet tall.
House is 0 feet tall.
Overloaded method: House  is 0 feet tall.
Share on Google Plus

About Yomal Malinda

    Blogger Comment
    Facebook Comment


Post a Comment

We value your comments... Whether it is Good or Bad they are the only thing which we can get from you guys...! :) :) Please be kind enough to stop by and comment something about this post....!

Powered by Blogger.