Overloading method (contd) Previous
Concept of overloading : note the signature is changed in overloading

class something
{
void method_1() { do something }
void method_1(int i) { do something }
void method_1(String str) { do something }
void method_1(String str, int i) { do something }
}

Concept of overriding : note the signature is changed in overloading

Signature like method_1() remains same.
void method_1() { do something different than base}

Concept overriding and overloading together

  • void method_1() {  default }
  • void method_1(String str) { signature (over-rload, and override print messsage ) }

Overloading a constructor

  • class test
    {

     public test()
    {
     this.test = "Hello from C#.";
    }

    public test(String message)
    {
     this.test = message;
    }}

Overriding a constructor

  • class test
    {
    int n1 =10 , n2 =20;
     public test()
    {
      return ( n2/n1);
    }

    public test(String message)
    {
     this.test = message;
    }}

 

In this example, the overloaded method has different parameter than that of the base class.

import java.util.*;
import java.io.*;
//javac clas_name.java
public class test
{
private static int player_no = 12;
public test()
{ System.out.println("This is a constructor with no param");
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Type something : ");
String str = "";
str = br.readLine();
method_client mc = new method_client();
mc.method_1(str);
}
}
class method_server
{
private int n1 = 12;
public method_server() { System.out.println("\tserver constructor"); }
public void method_1(int n1)
{
System.out.println("\tmethod server : " );
System.out.println("\tPrint server variable n1 : " + this.n1);
this.n1 = n1;
System.out.println("\tPrint server variable n1 : " + this.n1);

}
}
class method_client extends method_server
{
private int n1 = 24;
public method_client(String str){ System.out.println("client constructor"); }
public void method_1(String str)
{
System.out.println("client : " + str);
System.out.println("Print client variable : " + n1);
this.method_1(n1);
}
}

Example : 2

import java.util.*;
import java.io.*;
//javac clas_name.java
public class test
{
private static int player_no = 12;
public test()
{ System.out.println("This is a constructor with no param");
}
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Type something : ");
String str = "";
str = br.readLine();
method_client mc = new method_client(str);
method_server dc = new method_server();
mc.method_1();
mc.method_1(30);
mc.method_1(30, str);



}
}
class method_server
{
private int n1 = 12;
public method_server() { System.out.println("\tserver constructor"); }
public void method_1(int n1)
{
System.out.println("\tmethod server : " );
System.out.println("\tPrint server variable n1 : " + this.n1);
this.n1 = n1;
System.out.println("\tPrint server variable n1 : " + this.n1);

}
}
class method_client extends method_server
{
private int n1 = 24;
String str;
public method_client(){ System.out.println("client default constructor");}
public method_client(String str){ System.out.println("client overloaing constructor");
this.str = str;}
public void method_1()
{
System.out.println("default method in client : " + str);
System.out.println("default method in client Print variable : " + n1);
//this.method_1(n1);
}
//overloading a method
public void method_1(int n1)
{

//overriding a method
System.out.println("client overloadded method accepts integer : " + n1);
}
//overloading a method
public void method_1(int n1, String str)
{
//overriding a method
System.out.println("client overloadded method accepts integer and string: " + n1 + " "+ str);
}
}