COMPUTER 2430 Object Oriented Programming and Data Structures I
Class Person is a subclass of Object public class Person { protected String name; public Person(String s) name = s; } @Override public String toString() return name; Class Person is a subclass of Object Overrides toString
Student is a subclass of Person public class Student extends Person { private float gpa; public Student( String s, float value) super(s); gpa = value; } public boolean setGPA (float value) public float getGPA() @Override public String toString() return "Student " + name + " has a GPA of " + gpa; Student is a subclass of Person Overrides toString
Professor is a subclass of Person public class Professor extends Person { private String discipline; public Professor(String s, String d) super(s); discipline = d; } @Override public String toString() return "Professor " + name + " is in discipline of " + discipline; Professor is a subclass of Person Overrides toString
Subclasses override methods toString public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); } Subclasses override methods toString
The compiler will check this public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p.setGPA(3.3F); // Invalid s.setGPA(3.3F); // Valid f.setGPA(3.3F); // Invalid } For a class variable, only methods of the class can be called on the variable. The compiler will check this
public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); s = p; // Valid? }
An instance of base class may not be an instance of its subclasses public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); s = p; // Invalid System.out.println(s.getGPA()); } An instance of base class may not be an instance of its subclasses
public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid? }
public static void main(String[] args) Person p = new Person("Frank"); public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid p.anyBaseClassMethod(); } An instance of subclasses is an instance of its base class And inherits all methods of the base class
public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid p.toString(); // What’s the output? }
public static void main(String[] args) Person p = new Person("Frank"); public class CS2430Sx { public static void main(String[] args) Person p = new Person("Frank"); System.out.println(p.toString()); Student s = new Student("Yang", 3.5F); System.out.println(s.toString()); Professor f = new Professor("Qi", "CS"); System.out.println(f.toString()); p = s; // Valid p.toString(); // What’s the output? } String from the subclass: Run-Time Bindding Object-Oriented Programming!
Inheritance Variables of subclasses cannot point to instances of its base class Variables of base class can point to instances of its subclasses You can use an instance of child class wherever a instance of parent class is expected (Not the other way around!) Run time binding: the instance determine which version of a method will be fired if the method is overridden in the subclass
Unified Modeling Language (UML) Very simple UML class diagram One way association (A “uses” B) (More specific in CS3430) Quiz is coming? Program due! A B
Unified Modeling Language (UML) Very simple UML class diagram One way association (A “uses” B) (More specific in CS3430) Quiz is coming? Program due! Prog0 ScoresList
Unified Modeling Language (UML) Very simple UML class diagram Ignore the data and methods Show class name only A inherits B Quiz is coming? Program due! A B
UML Person Student Quiz is coming? Program due! Professor
Lab 2 Part of Prog1 Five (5) points Due 10 pm, Wednesday, September 27 Must use the SE Tool to log your time as part of Prog1 https://alpha.ion.uwplatt.edu/CSSE/Account/Login Click Here
Lab2 Two files are at K:\Courses\CSSE\yangq\cs2430\1CourseMaterials\Lab2 GolfLeagueMember.java Golfer.java