Computer Science 225
Advanced Programming
Spring 2017, Siena College
Overriding BlueJ Project
Click here to download a BlueJ project for Overriding.
Overriding Source Code
The Java source code for Overriding is below. Click on a file name to download it.
/* * Example Overriding: overriding methods in subclasses * * Jim Teresco, Siena College, Computer Science 225, Spring 2017 * * Based heavily on example from Darren Lim */ import java.util.ArrayList; class Student { public String name = "Fred"; protected void getName() { System.out.println(name); } } class Undergraduate extends Student { @Override protected void getName() { System.out.println("Undergraduate : " + name); } } class Freshman extends Undergraduate { } class Graduate extends Student { @Override protected void getName() { System.out.println("Graduate : " + name); } } class Phd extends Graduate { @Override protected void getName() { System.out.println("Phd : " + name); } } public class Overriding { public static void main(String[] args) { // populate an ArrayList with Student objects and instances of derived classes ArrayList<Student> students = new ArrayList<>(); students.add(new Student()); students.add(new Undergraduate()); students.add(new Freshman()); students.add(new Graduate()); students.add(new Phd()); // let's call each one's getName method for (Student s : students) { s.getName(); } } }