Created
April 17, 2015 20:39
-
-
Save kchodorow/ef4bb3f33625f569d064 to your computer and use it in GitHub Desktop.
Friday's class example
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
import java.util.ArrayList; | |
public class Runner { | |
public static void main(String[] args) { | |
Person msM = new Teacher("Ms.", "M"); | |
ArrayList<Person> people = new ArrayList<Person>(); | |
people.add(msM); | |
msM.getName(); | |
((Teacher) msM).assignHomework(); | |
// Dynamic typing. | |
Person anything; | |
if (Math.random() < .5) { | |
anything = new Teacher("Ms", "X"); | |
} else { | |
anything = new Student("Foo"); | |
} | |
Teacher msC = new Teacher("Mrs", "C"); | |
msC.getName(); | |
msC.assignHomework(); | |
// Downcasting. | |
Person offDuty = msC; | |
System.out.println(msC.getName()); | |
Person p = new Person("blah"); | |
System.out.println(p.getName()); | |
// Method overloading. | |
addStuff(3, 4); | |
addStuff(1,"s"); | |
Person p2 = people.get(0); | |
p2.getName(); | |
} | |
public static void addStuff(int x, int y) { | |
System.out.println(x+y); | |
} | |
public static void addStuff(int x, String z) { | |
System.out.println(x+z); | |
} | |
} | |
class Person { | |
String name; | |
Person(String inputName) { | |
name = inputName; | |
} | |
public String getName() { | |
return name; | |
} | |
} | |
class Teacher extends Person { | |
String title; | |
Teacher(String inputTitle, String inputName) { | |
super(inputName); | |
title = inputTitle; | |
} | |
public void assignHomework() { | |
} | |
// Method overriding. | |
public String getName() { | |
return title +". "+ super.getName(); | |
} | |
} | |
class Student extends Person { | |
// Constructor. | |
Student(String inputName) { | |
super(inputName); | |
} | |
// Non-constructor method. | |
public void doHomework() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment