Skip to content

Instantly share code, notes, and snippets.

@jaekwon
Created May 22, 2015 15:33
Show Gist options
  • Save jaekwon/8025b9f3a482b3219a21 to your computer and use it in GitHub Desktop.
Save jaekwon/8025b9f3a482b3219a21 to your computer and use it in GitHub Desktop.
Example of Java inheritance that overrides a baseclass's behavior
import java.util.*;
import java.lang.*;
import java.io.*;
class Human {
public Human() {}
public void walk() {
this.moveFeet();
}
public void moveFeet() {
System.out.println("Human.moveFeet");
}
}
class Dork extends Human {
public Dork() {}
public void moveFeet() {
System.out.println("Dork.moveFeet");
}
}
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Dork d = new Dork();
d.walk(); // Dork.moveFeet
Human h = new Dork();
h.walk(); // Dork.moveFeet
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment