Skip to content

Instantly share code, notes, and snippets.

@Nkzn
Created May 4, 2014 16:12
Show Gist options
  • Select an option

  • Save Nkzn/9e8e6221a0803d1c4c05 to your computer and use it in GitHub Desktop.

Select an option

Save Nkzn/9e8e6221a0803d1c4c05 to your computer and use it in GitHub Desktop.
public class HelloWorld {
public static void main (String[] args) {
User tom = new User("tom");
SubUser bob = new SubUser("bob");
tom.sayHi();
bob.sayHi(); // オーバーライドしたのでUserと挙動が違うはず
}
}
class User {
String name;
String email;
User(String name) {
this.name = name;
}
void sayHi() {
System.out.println("Hi! I'm " + this.name);
}
}
class SubUser extends User {
SubUser(String name) {
super(name); // User(String name)のコンストラクタを呼び出している
}
@Override
void sayHi() {
System.out.println("HIIII!!! I'm " + this.name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment