Skip to content

Instantly share code, notes, and snippets.

@kdabir
Created February 1, 2014 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kdabir/8750534 to your computer and use it in GitHub Desktop.
Save kdabir/8750534 to your computer and use it in GitHub Desktop.
Differences between java and groovy method dispatch. Compare the output of both programs
Main Person: I am a Person
Main Employee: I am an Employee
Main Employee: I am an Employee
Person Printer: I am a Person
Person Printer: I am an Employee
Person Printer: I am an Employee
Person Printer: I am a Person
Employee Printer: I am an Employee
Employee Printer: I am an Employee
Person Printer: I am a Person
Employee Printer: I am an Employee
Employee Printer: I am an Employee
Main Person: null
Main Person: I am a Person
Main Employee: I am an Employee
Main Person: I am an Employee
Person Printer: I am a Person
Person Printer: I am an Employee
Person Printer: I am an Employee
Person Printer: I am a Person
Employee Printer: I am an Employee
Person Printer: I am an Employee
Person Printer: I am a Person
Person Printer: I am an Employee
Person Printer: I am an Employee
Main Employee: null
public class Main {
void prettyPrint(Person p) {
System.out.println("Main Person: " + p);
}
void prettyPrint(Employee e) {
System.out.println("Main Employee: " + e);
}
public static void main(String[] args) {
final Person p = new Person();
final Employee e = new Employee();
final Person pe = new Employee();
final Main main = new Main();
main.prettyPrint(p);
main.prettyPrint(e);
main.prettyPrint(pe);
final PersonPrinter personPrinter = new PersonPrinter();
personPrinter.print(p);
personPrinter.print(e);
personPrinter.print(pe);
final EmployeePrinter employeePrinter = new EmployeePrinter();
employeePrinter.print(p);
employeePrinter.print(e);
employeePrinter.print(pe);
final PersonPrinter personEmployeePrinter = new EmployeePrinter();
personEmployeePrinter.print(p);
personEmployeePrinter.print(e);
personEmployeePrinter.print(pe);
main.prettyPrint(null);
}
}
class Person {
public String toString() {
return "I am a Person";
}
}
class Employee extends Person {
public String toString() {
return "I am an Employee";
}
}
class PersonPrinter {
public void print(Person p) {
System.out.println("Person Printer: " + p);
}
}
class EmployeePrinter extends PersonPrinter {
public void print(Employee p) {
System.out.println("Employee Printer: " + p);
}
}
public class Main {
void prettyPrint(Person p) {
System.out.println("Main Person: " + p);
}
void prettyPrint(Employee e) {
System.out.println("Main Employee: " + e);
}
public static void main(String[] args) {
final Person p = new Person();
final Employee e = new Employee();
final Person pe = new Employee();
final Main main = new Main();
main.prettyPrint(p);
main.prettyPrint(e);
main.prettyPrint(pe);
final PersonPrinter personPrinter = new PersonPrinter();
personPrinter.print(p);
personPrinter.print(e);
personPrinter.print(pe);
final EmployeePrinter employeePrinter = new EmployeePrinter();
employeePrinter.print(p);
employeePrinter.print(e);
employeePrinter.print(pe);
final PersonPrinter personEmployeePrinter = new EmployeePrinter();
personEmployeePrinter.print(p);
personEmployeePrinter.print(e);
personEmployeePrinter.print(pe);
main.prettyPrint(null);
}
}
class Person {
public String toString() {
return "I am a Person";
}
}
class Employee extends Person {
public String toString() {
return "I am an Employee";
}
}
class PersonPrinter {
public void print(Person p) {
System.out.println("Person Printer: " + p);
}
}
class EmployeePrinter extends PersonPrinter {
public void print(Employee p) {
System.out.println("Employee Printer: " + p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment