Created
September 26, 2017 13:45
-
-
Save Koziolek/1b522013c308b2b8eeb1f8106da6ab40 to your computer and use it in GitHub Desktop.
Which method `sayHellow` we call?
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
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