Overloading:
Java allows you to define two or more methods with the same name and
different parameter list in one class.
Defining two or more methods with the same in one class are called as Method Overloading.
package com.jsl.core;
public class OverLoadingDemo {
public int add(int a,int b){
return a+b;
}
public int
add(int a,int b,int c){
return a+b;
}
public int
add(String a,String b){
return Integer.parseInt(a)+Integer.parseInt(b);
}
}
Overloading also
known as static binding or early binding or compile-tem polymorphism. The
overloading method is binded with respective to reference variable by compiler
based on method arguments.
package
com.jsl.core;
public class
MathOperations {
public int
sum(int a,int b){
return
a+b;
}
public float
sum(float a,float b){
return
a+b;
}
public static void
main(String[] args) {
MathOperations obj=new
MathOperations();
System.out.println("The
sum is :" +obj.sum(10, 10.5f));
System.out.println("The
sum is :"+obj.sum(10, 10));
System.out.println("The
sum is :"+obj.sum(10.5f, 10.5f));
}
}
Output:
The sum is :20.5
The sum is :20
The
sum is :21.0
In case of method overloading, at the time of
compilation compiler look for exact match parameter method. If method doesn’t
have with exact matched parameter then compiler try to promotes the parameters.
package
com.jsl.core;
public class
MathOperations {
public double
sum(double a,double b){
return
a+b;
}
public static void
main(String[] args) {
MathOperations obj=new
MathOperations();
double res=obj.sum('a', 'a');
System.out.println("The
sum is :" +res);
}
}
Output:
The sum is :194.0
Here at the
time of compilation the compiler try to find the exact match parameter method obj.sum('a', 'a') but there is no method with
two characters, then compiler promotes character parameters double type and
bind that method with the reference variable.
Over
loading with objects:
package
com.jsl.core;
public class
Member {
private int id;
private
String name;
Member(int
id,String name){
this.id=id;
this.name=name;
}
@Override
public
String toString() {
return "Id
is : "+id+" Name is : "+name;
}
}
package
com.jsl.core;
public class
MethodOverloading {
public void
showInfo(Object obj){
System.out.println(obj);
}
public void
showInfo(Member obj){
System.out.println(obj);
}
public static void
main(String[] args) {
MethodOverloading obj=new
MethodOverloading();
obj.showInfo(new
Object());
obj.showInfo("JSL
TECH");
obj.showInfo(new
Member(1001, "Lakshman"));
}
}
Output:
java.lang.Object@addbf1
JSL TECH
Id is : 1001 Name is : Lakshman
Here there is no method with the showInfo( ) with
string parameter. The compiler promotes to super class type and invokes the
object class method.
Example:
package
com.jsl.core;
public class
MethodOverloading {
public void showInfo(String
name){
System.out.println(name);
}
public void
showInfo(StringBuffer name){
System.out.println(name);
}
public static void
main(String[] args) {
MethodOverloading obj=new
MethodOverloading();
obj.showInfo(new
Object());
}
}
Here it leads to compilation error, because object()
can’t be caste to the String or StringBuffer type. So leads compilation error.
Overriding:
Sometimes you
inherit a method that doesn't meet the requirements for a subclass. When this happens,
you can override the method in subclass.
The rules for overriding as follows:
·
The
overriding method must have same name and same parameter list.
·
The
overriding method access specifier should be same or less restrictive; it
should not be more restrictive.
Private
--------> (default) -------> protected ---------> public
·
Return
type must be same, except co-variant type.
·
In
the overriding method declaration, the exception should be same exception or
without exception or narrow exception. It
shouldn’t be new or border exception.
·
The
static method can’t be override as non static method.
Example:
package
com.jsl.core;
class
Person{
private int id;
private
String name;
Person(int
id,String name){
this.id=id;
this.name=name;
}
@Override
public
String toString() {
return "The
id is : "+id+"\nThe name is :"+name;
}
}
public class
OverridingDemo {
public static void
main(String[] args) {
Person person=new
Person(1001, "JSL Tech");
System.out.println(person);
}
}
Output:
The id is : 1001
The name is :JSL Tech
The toString( )
method is object class method which return the string. By default it returns
ClassName+hashCode, but in our case we overriding the toString method in Person
class which return the object content is the form of String.
Co-variant type in method overriding example
package
com.jsl.core;
class
One{
public
Number getValue(){
return
1000;
}
}
class Two
extends One{
@Override
public
Float getValue(){
return
3000.5f;
}
}
public class
OverridingDemo {
public static void
main(String[] args) {
One obj=new
Two();
System.out.println("The
value is :"+obj.getValue());
}
}
Output:
The value is: 3000.5
Here the super class method return
type is Number class, and in the sub class method return type is Float. All the
numbered class super class is object class. This is called co-variant type.
Overriding
method can’t reduce the visibility:
package
com.jsl.core;
class
One{
protected void
display(){
System.out.println("The
display method one");
}
void
show(){
System.out.println("The
show method one");
}
public void
print(){
System.out.println("The
print method of one");
}
}
class Two
extends One{
@Override
public void
display() {
System.out.println("The
display method of two");
}
@Override
protected void
show(){
System.out.println("The
show method of two");
}
@Override
public void
print() {
System.out.println("The
print method of two");
}
}
Here the default show( ) method
is overridden as the protected show( ) method. In overriding method we should
give same acess
Overloading
|
Overriding
|
Writing two or
more methods with same name and different parameter list(i.e order or number of
parameter must be different) in same class is called overloading
|
Writing super
class methods in the sub class with same name and same parameter list is
called method overriding.
|
Return type
can be change
|
Return type
must be same, except co-variant type
|
Access
specifier can be change
|
Access
Specifier should be same or less restrictive he should not be more restrictive
|
The exception
can be change
|
The exception
should be same exception or without exception or narrow exception. It shouldn’t be new or border exception.
|