Java Method Overloading & Java Method Overriding

Java Method Overloading
An overloaded method is more than one method: it is two or more separate methods using the same
method name( of course, with different parameters ).
Methods are defined in classes. Methods are distinguished by the compiler by their method signatures.
A method signature includes method name, number of parameters and their type. But in procedural
languages like in C, method overloading is not permitted.

Method overloading allows to group methods of same functionality under one name. Always the name
of the method should reflect its functionality( that is, what is it going to give us). In method overloading,
compiler does not consider return type in differentiating the methods.
Example:-
//Start Of MethodOverLoading Class
public class MethodOverLoading
{
 //Start of add(int i, int j) interger method
public void add(int i, int j)
{
int k = i+j;
System.out.println("Addition is :" +k);
 }//End of add() interger Method
 //Start of  add(String s, String t) String Method
 public void add(String s, String t)
 {
     int k = Integer.parseInt(s) + Integer.parseInt(t);
     System.out.println("Addition is :" +k);
   }//End of add() string Method
}//End of MethodOverLoading class
//Start of Main Class
class Main
{
//Start Of Main Method
  public static void main(String args[])
  {
   //Create MethodOverLoading object
MethodOverLoading obj = new MethodOverLoading();

//call add() and pass two values integer type
obj.add(10,30);
//call add() and pass two values string types
obj.add("20", "40");
  }//End of Main Method
 }//End of Main class
Save this file as Main.java
To compile: javac Main.java
To execute: java Main
Output:-

Addition is : 40
Addition is : 60
Java Method Overriding
If the method signatures of a method in superclass and subclass are same, we say superclass method
is overridden in subclass. In method overriding the return type also must be same of that of superclass.
The subclass method must have the same name, parameter list and return type as the superclass
method. That is, by using the same method name of the superclass, the subclass gives different output.
When you extend a class and write a method in the derived class which is exactly similiar to the one
present in the base class, it is termed as overriding.
Example:-
//Start Of BaseClass
public class BaseClass
{
//Start of MethodOverriding()
public void MethodOveriding()
{
System.out.println("Base Class Method");
}  //End of MethodOverriding()
} //End of BaseClass
 //Start of DerivedClass
public class DerivedClass extends BaseClass
{
 //Start of MethodOverriding()
public void MethodOverriding()
{
System.out.println("Derived Class Method");
}//End of MethodOverriding()
}//End of DerivedClass
//Start of Main Class
class Main
{
//Start of Main Method
public static void main(String args[])
{
//Create DerivedClass object
DerivedClass obj = new DerivedClass();
obj.MethodOverriding();
}//End of Main Method
}//End of Main Class
Save this file as Main.java
To compile: javac Main.java
To execute: java Main
Output:-
Derived Class Method

No comments:

Post a Comment