Created
February 1, 2014 10:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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