Download presentation
Presentation is loading. Please wait.
Published byGwendoline Wood Modified over 6 years ago
1
COMPUTER 2430 Object Oriented Programming and Data Structures I
2
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
3
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
4
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
5
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
6
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
7
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? }
8
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
9
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? }
10
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
11
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? }
12
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!
13
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
14
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
15
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
16
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
17
UML Person Student Quiz is coming? Program due! Professor
18
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 Click Here
19
Lab2 Two files are at K:\Courses\CSSE\yangq\cs2430\1CourseMaterials\Lab2 GolfLeagueMember.java Golfer.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.