Overloading a Method Next
Key concept of this example
  • overloading a method
    • super class original method()--- no parameter
    • derived class original method(variable-type variable)
  • this key word to a method
    • this refer to super class
    • this.method() --refers to superclass.method
    •  
  • use of overloaded methods  :
    1.  in the same class or a subclass
    2. the method in the derived class, would have the same method name used in a base class
    3. the method in the derived class, would have the same name, but would have different parameter lists,
    4. the method in the derived class, would have the same name, can have different return types .

Also see overloading a constructor: Link

Using Eclipse

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()
{
System.out.println("\tmethod server : " );
System.out.println("\tPrint server variable : " + n1);
}
}
class method_client extends method_server
{
private int n1 = 24;
public method_client(){ 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();
}
}

using command line

import java.util.*;
import java.io.*;
//javac overloading_method.java
public class overloading_method
{
private static int player_no = 12;
public overloading_method()
{ 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()
{
System.out.println("\tmethod server : " );
System.out.println("\tPrint server variable : " + n1);
}
}
class method_client extends method_server
{
private int n1 = 24;
public method_client(){ 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();
}
}