Skip to content

Instantly share code, notes, and snippets.

@Koziolek
Created September 26, 2017 13:45
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 Koziolek/1b522013c308b2b8eeb1f8106da6ab40 to your computer and use it in GitHub Desktop.
Save Koziolek/1b522013c308b2b8eeb1f8106da6ab40 to your computer and use it in GitHub Desktop.
Which method `sayHellow` we call?
package com.luxoft.oop;
public class HelloWorld {
public static void main(String[] args) {
MessagePrinter printer = new MessagePrinter();
Message msg = new Message("I am message");
Greetings grt = new Greetings("I am greetings");
Message msgAsGrt = new Greetings("I am greetings");
System.out.println(printer.sayHallo(msg));
System.out.println(printer.sayHallo(grt));
System.out.println(printer.sayHallo(msgAsGrt));
System.out.println(printer.sayHallo((Greetings)msgAsGrt));
}
}
class MessagePrinter {
public String sayHallo(Message msg) {
return msg.getText();
}
public String sayHallo(Greetings msg) {
return msg.getText();
}
}
class Message {
private String text;
public Message(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
class Greetings extends Message {
public Greetings(String text) {
super("Hello! I have message for you: " + text);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment