logo codesdope

More about methods


In this chapter we will learn about :
1. Method overloading
2. Method overriding

So, let's first start with method overloading.

Method overloading


Same as constructors, we can also overload methods. Conditions for method overloading are:-
1. Methods to be overloaded must have the same name.
2. All methods must have different arguments( either different number of parameters or different type of parameters ).

Let's see an example


class Rectangle{

  public static void printArea(int x,int y){
    System.out.println(x*y);
  }
  public static void printArea(int x){
    System.out.println(x*x);
  }
  public static void printArea(int x,double y){
    System.out.println(x*y);
  }
  public static void printArea(double x){
    System.out.println(x*x);
  }

  public static void main(String[] args){
    printArea(2,4);
    printArea(2,5.1);
    printArea(10);
    printArea(2.3);
}
}
Output
8
10.2
100
5.289999999999999

Here, we have defined four methods with the same name 'printArea' but different parameters.
In the main class, firstly the function printArea is called with 2 and 4 passed to it. Since both 2 and 4 are integers, so the method named printArea with both its parameters of type int (int x, int y) will be called.
After that, the second method is called with 2 and 5.1 passed to it. Since 2 is of type int and 5.1 is of type double, so the method with the first parameter of type int and the second one of type double(int x,double y) will be called.
Similarly after that, the method with only one integer value as its parameter will be called and at last, the method with a single double value as its parameter will be called.

One thing to be noted here is that we can make any class the main class by having the main method to it.

Method Overriding


To understand method overriding, let's first look at an example.

class Animals{
  public void sound(){
    System.out.println("This is parent class.");
  }
}
class Dogs extends Animals{
  public void sound(){
    System.out.println("Dogs bark");
  }
}
class m{
public static void main(String[] args){
  Dogs d = new Dogs();
  d.sound();
}
}
Output
Dogs bark

In the above example, the class 'Dogs' and its parent class 'Animals' have the same method public void sound(). When the object 'd' of the class Dogs calls this method, then the method in the child class 'Dogs' is called, not that in the parent class. Thus, the method in the child class overrides the method in the parent class.

This is called method overriding.

Conditions for Method Overriding

  • Methods of both parent and child class must have the same name.
  • Methods must have the same argument list and return type.
  • A method declared static cannot be overridden.
  • If a method cannot be inherited, it cannot be overridden.
Constructors cannot be overridden.

Let's see some another example on method overriding.

class Animals{
  public void sound(){
    System.out.println("This is parent class.");
  }
}
class Dogs extends Animals{
  public void sound(){
    System.out.println("Dogs bark");
  }
}
class Cats extends Animals{
  public void sound(){
    System.out.println("Cats meow");
  }
}
class Monkeys extends Animals{
  public void sound(){
    System.out.println("Monkeys whoop.");
  }
}
class m{
public static void main(String[] args){
  Animals d = new Dogs();
  Animals c = new Cats();
  Animals m = new Monkeys();
  d.sound();
  c.sound();
  m.sound();
}
}
Output
Dogs bark
Cats meow
Monkeys whoop.

This is the same as the previous example. Just we have multiple subclasses of the parent class Animals.
The objects 'b', 'c' and 'd' of the classes Dogs, Cats and Monkeys respectively call the method 'public void sound()' of their respective classes. Thus the methods of the three classes override the same method of the parent class.

First of all, convince yourself that you are the best because rest of your life is going to proving this to others
-Wasim Akram

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.