Created
May 22, 2015 15:33
-
-
Save jaekwon/8025b9f3a482b3219a21 to your computer and use it in GitHub Desktop.
Example of Java inheritance that overrides a baseclass's behavior
This file contains hidden or 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
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