Inheritance – Student Exercise
Use this implementation of Student and StudentAthlete ADTs to show inheritance concepts.
- Describe what the Student ADT looks like:
A good description should include the name of the instance fields, whether the ADT has constructor(s), the methods’ purpose, and names. - Describe what the StudentAthlete ADT looks like:
A good description should include the name of the instance fields, whether the ADT has constructor(s), the methods’s purpose, and names.
Mostly clearly identify attributes, any overridden methods, any methods that belong to the child class. As a final sentence describe the relationship between Student and StudentAthlete. - Describe the Academia driver class.
- Add accessor and mutator methods for the studentName and sportName. NOTE: do not change any visibility specifier.
- In the Academia driver class, include calls to the methods you just implemented.
public class Student { private String name; private int numCourses; //----------------------------------------------------------------- // Sets up a student with the specified name and number of // courses. //----------------------------------------------------------------- public Student (String studentName, int courses) { name = studentName; numCourses = courses; } //----------------------------------------------------------------- // Returns information about this student as a string. //----------------------------------------------------------------- public String toString() { String result = "Student name: " + name + "\n"; result += "Number of courses: " + numCourses; return result; } }
//******************************************************************** // StudentAthlete.java Author: Lewis/Loftus/Cocking // // Represents a student athlete who plays a sports team for the school. // Used to demonstrate inheritance. //******************************************************************** public class StudentAthlete extends Student { private String sport; //----------------------------------------------------------------- // Sets up the student athlete using the specified information. //----------------------------------------------------------------- public StudentAthlete (String studentName, int courses, String sportName) { super (studentName, courses); sport = sportName; } //----------------------------------------------------------------- // Returns a description of this graduate student as a string. //----------------------------------------------------------------- public String toString() { String result = super.toString(); result += "\nSport: " + sport; return result; } }
//******************************************************************** // Academia.java Author: Lewis/Loftus/Cocking // // Demonstrates the use of methods inherited from the Object class. //******************************************************************** public class Academia { //----------------------------------------------------------------- // Creates objects of two student types, prints some information // about them, then checks them for equality. //----------------------------------------------------------------- public static void main (String[] args) { Student Frank = new Student ("Frank", 5); StudentAthlete Suki = new StudentAthlete ("Suki", 4, "Soccer"); System.out.println (Frank); System.out.println (); System.out.println (Suki); System.out.println (); if (! Frank.equals(Suki)) System.out.println ("These are two different students."); } }
All source: Java Software Solutions by Lewis, Loftus and Cocking