Credits

Monday, November 29, 2010

More of Java's Method Overloading

 It is also possible that we call methods in different classes simply by creating their respective instances.
The program below allows us to use method overload where printGreetings () are both used in two separate classes. Provided that we identify which source the method is called, the program will simply run successfully.


[+/-] show/hide



/*with super in constructor*/
/*calling other classes OverloadMethodParent and Base Classes */
public class OverloadMethodv2{

public static void main (String args[]){

OverloadMethodParentClassv2 myObject1 = new OverloadMethodParentClassv2("Yuri");
myObject1.printGreetings();
OverloadMethodBaseClassv2 myObject2 = new OverloadMethodBaseClassv2("Rosilie");
myObject2.printGreetings();
myObject2.printThankYou();

}

}


/*with super in constructor*/
import javax.swing.*;
public class OverloadMethodParentClassv2{
String name;

OverloadMethodParentClassv2(String xname)
{
name=xname;
}

void printGreetings()
{
JOptionPane.showMessageDialog(null,"Hello "+name);
}


}



/*with super in constructor*/
import javax.swing.*;

public class OverloadMethodBaseClassv2 extends OverloadMethodParentClassv2{
String xname;
OverloadMethodBaseClassv2(String myname)
{
super(myname);
xname=myname;

}

void printGreetings()
{
JOptionPane.showMessageDialog(null,"Hi "+xname);

}

void printThankYou()
{
JOptionPane.showMessageDialog(null,"Thank you "+xname);

}
}





AddThis