[go: up one dir, main page]

0% found this document useful (0 votes)
87 views6 pages

Method Overloading: Three Ways To Overload A Method

Method overloading in Java allows a class to have multiple methods with the same name but different parameters. There are three ways to overload a method: 1) by having a different number of parameters, 2) by having parameters of different data types, or 3) by having parameters of the same types but in a different sequence. Method overriding allows a subclass to provide its own implementation of an inherited method from the parent class. This allows the subclass to customize the parent's behavior. When a parent class reference refers to a child class object, the overridden method is determined at runtime based on the object's type, in a process known as dynamic method dispatch.

Uploaded by

Agniva Chowdhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views6 pages

Method Overloading: Three Ways To Overload A Method

Method overloading in Java allows a class to have multiple methods with the same name but different parameters. There are three ways to overload a method: 1) by having a different number of parameters, 2) by having parameters of different data types, or 3) by having parameters of the same types but in a different sequence. Method overriding allows a subclass to provide its own implementation of an inherited method from the parent class. This allows the subclass to customize the parent's behavior. When a parent class reference refers to a child class object, the overridden method is determined at runtime based on the object's type, in a process known as dynamic method dispatch.

Uploaded by

Agniva Chowdhury
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

METHOD OVERLOADING

Method Overloading is a feature that allows a class to have more than one method
having the same name, if their argument lists are different. It is similar to constructor
overloading in Java, that allows a class to have more than one constructor having
different argument lists. let’s get back to the point, when I say argument list it means the
parameters that a method has: For example the argument list of a method add(int a, int
b) having two parameters is different from the argument list of the method add(int a, int
b, int c) having three parameters.

THREE WAYS TO OVERLOAD A METHOD


In order to overload a method, the argument lists of the methods must differ in either of
these:
1. Number of parameters.
For example: This is a valid case of overloading
add(int, int)
add(int, int, int)

2. Data type of parameters.


For example:

add(int, int)
add(int, float)

3. Sequence of Data type of parameters.


For example:

add(int, float)
add(float, int)

EXAMPLES OF METHOD OVERLOADING


Example 1: Overloading – Different Number of parameters in argument
list
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);

Output:

a
a 10

Example 2: Overloading – Difference in data type of parameters


class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}

class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
}
}
Output:

a
5

ADVANTAGES OF METHOD OVERLOADING


1. Overloading in Java is the ability to create multiple
methods of the same name, but with different parameters.
2. The main advantage of this is cleanliness of code.
3. Method overloading increases the readability of the
program.
4. Overloaded methods give programmers the flexibility to
call a similar method for different types of data.
5. Overloading is also used on constructors to create new
objects given different amounts of data.
6. You must define a return type for each overloaded
method. Methods can have different return types
METHOD OVERRIDING
Declaring a method in sub class which is already present in parent
class is known as method overriding. Overriding is done so that a
child class can give its own implementation to a method which is
already provided by the parent class. In this case the method in
parent class is called overridden method and the method in child class
is called overriding method. In this guide, we will see what is method
overriding in Java and why we use it.

EXAMPLES OF METHOD OVERRIDING


Lets take a simple example to understand this. We have two
classes: A child class Boy and a parent class Human.
The Boy class extends Human class. Both the classes have a
common method void eat(). Boy class is giving its own
implementation to the eat() method or in other words it is
overriding the eat() method..The purpose of Method Overriding
is clear here. Child class wants to give its own implementation
so that when it calls this method, it prints Boy is eating instead
of Human is eating.

class Human{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
public static void main( String args[]) {
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
Output:
Boy is eating
ADVANTAGES OF METHOD OVERRIDING
The main advantage of method overriding is that the class can give its
own specific implementation to a inherited method without even
modifying the parent class code. This is helpful when a class has
several child classes, so if a child class needs to use the parent class
method, it can use it and the other classes that want to have different
implementation can use overriding feature to make changes without
touching the parent class code.

METHOD OVERRIDING AND DYNAMIC METHOD


DISPATCH
Method Overriding is an example of runtime polymorphism. When a
parent class reference points to the child class object then the call to
the overridden method is determined at runtime, because during
method call which method(parent class or child class) is to be
executed is determined by the type of object. This process in which
call to the overridden method is resolved at runtime is known as
dynamic method dispatch. Lets see an example to understand this.

class ABC{
//Overridden method
public void disp()
{
System.out.println("disp() method of parent class");
}
}
class Demo extends ABC{
//Overriding method
public void disp(){
System.out.println("disp() method of Child class");
}
public void newMethod(){
System.out.println("new method of child class");
}
public static void main( String args[]) {
/* When Parent class reference refers to the parent class
object
* then in this case overridden method (the method of parent
class)
* is called.
*/
ABC obj = new ABC();
obj.disp();

/* When parent class reference refers to the child class


object
* then the overriding method (method of child class) is
called.
* This is called dynamic method dispatch and runtime
polymorphism
*/
ABC obj2 = new Demo();
obj2.disp();
}
}
Output:

disp() method of parent class


disp() method of Child class
NAME : ARIJIT SADHUKHAN
STREAM : ELECTRICAL
ENGINEERING
SUBJECT: OBJECT ORIENTED
PROGRAMMING(TERM PAPER)
SUBJECT CODE: EE-604C
UNIQUE ID: 33201616023
YEAR: 3RD YEAR

You might also like