Download presentation
Presentation is loading. Please wait.
1
Lecture 6: Object-Oriented Design, Part 3
CompSci 230 Software Construction Lecture 6: Object-Oriented Design, Part 3
2
Agenda & Reading Topics: Reading
Inheritance, instantiation, and polymorphism Reading The Java Tutorials, on Inheritance Wikipedia, on Class Diagram COMPSCI 230: OOD2
3
Inheritance Observation: It’s common for real-life classes of objects to share a significant part of their attributes and behaviours E.g., lecturers and students all have names and ID numbers, go to uni, etc. E.g., both residential houses and office buildings have a floor area, doors, an address etc., can be locked or unlocked, vacated, painted, heated… However, even classes of objects which share a lot of attributes often differ in detail E.g., only lecturers get assigned to courses and lecture, only students enrol in courses E.g., only residential houses have bedrooms In many cases, we can identify that different classes of object also really belong to a common superclass E.g., both students and lecturers are university members, and every university member is a person E.g., both residential houses and office buildings are buildings Can associate certain attributes and behaviours with either the class itself or one of its more generic superclasses COMPSCI 230: OOD2
4
Inheritance example Person UniversityMember Lecturer Student
COMPSCI 230: OOD2
5
(public for expediency only…)
Inheritance example (public for expediency only…) public class Person { public String name; public void identify() { System.out.print("I'm acting as a person! "); System.out.println("My name is " + name); } public void moveAbout() { System.out.println("I'm movin' about!"); COMPSCI 230: OOD2
6
Like private, but also visible to code in subclasses
Inheritance example import java.util.*; // for the ArrayList class public class UniversityMember extends Person { public String auid; protected ArrayList<String> courses; public UniversityMember() { courses = new ArrayList<String>(); } public void identify() { System.out.print("I'm acting as a university member! "); System.out.print("My name is " + name + ". "); System.out.println("Gaudeamus igitur!"); Like private, but also visible to code in subclasses UniversityMember inherits name and moveAbout() from Person This is not shown in the class diagram (implied) UniversityMember overrides inherited identify() method with own declaration COMPSCI 230: OOD2
7
Inheritance example needed for Iterator
import java.util.*; public class Lecturer extends UniversityMember { public Lecturer() { super(); // call superclass constructor } public void assignToClass(String course) { courses.add(course); public void teach() { System.out.println("Teaching: "); for (Iterator<String> it = courses.iterator(); it.hasNext();) { System.out.println(it.next()); public void moveAbout() { System.out.println("I stride forward!"); needed for Iterator use UniversityMember() constructor override method inherited from Person COMPSCI 230: OOD2
8
Inheritance example use UniversityMember() constructor
import java.util.*; public class Student extends UniversityMember { public Student() { super(); } public void enrol(String course) { courses.add(course); public void study() { System.out.println("Studying: "); for (Iterator<String> it = courses.iterator(); it.hasNext();) { System.out.println(it.next()); public void moveAbout() { System.out.println("I'm milling around!"); use UniversityMember() constructor override method inherited from Person COMPSCI 230: OOD2
9
A bunch of new Java concepts
The keyword “super” refers to the current method in the superclass Here, we use it in Lecturer and Student to invoke the constructors of the superclass UniversityMember import: This lets us import classes (types) from a Java package Here, we import all classes from java.util with import java.util.*; These include ArrayList and Iterator An Iterator is an Interface that lets us read out elements of a list (including an ArrayList) one by one using its next() and hasNext() methods Think of an Interface as a set of methods and/or static variables that several unrelated classes implement. You can then use a variable whose type is that of the Interface to interact with objects of any of these classes. More on Interfaces soon. protected: a visibility modifier like private or public. protected acts like private wrt. code in unrelated classes, but like public wrt. code in subclasses COMPSCI 230: OOD2
10
Quick quiz to check our understanding
How many public methods can we invoke on a Student object? Why does the courses variable in the UniversityMember class have to be protected rather than private? UniversityMember is a superclass of … and a subclass of … If an object is of type Student, is it then also of type UniversityMember and of type Person? Can we use the auid field in an object of type UniversityMember if it’s referenced by a Person variable? What decides which version of moveAbout() or identify() gets used? COMPSCI 230: OOD2
11
Inheritance example COMPSCI 230: OOD2
public class UniversityExampleProgram { public static void main(String[] args) { System.out.println("*** Person object referenced by Person variable:"); Person p = new Person(); p.name = "Somebody"; p.identify(); p.moveAbout(); System.out.println("*** Student object referenced by Student variable:"); Student s = new Student(); s.name = "Studiosus"; s.identify(); System.out.println("*** Lecturer object referenced by Lecturer variable:"); Lecturer l = new Lecturer(); l.name = "Professaurus"; l.identify(); l.assignToClass("COMPSCI230"); // no problem l.teach(); // also no problem l.moveAbout(); System.out.println("*** Lecturer object referenced by Person variable:"); Person p2 = l; p2.identify(); // Person variable, but UniversityMember method() // p2.assignToClass("COMPSCI231"); // this won't compile // p2.teach(); // this won't compile either ((Lecturer) p2).assignToClass("COMPSCI280"); // explicit type cast ((Lecturer) p2).teach(); // explicit type cast } COMPSCI 230: OOD2
12
Inheritance example *** Person object referenced by Person variable:
System.out.println("*** Person object referenced by Person variable:"); Person p = new Person(); p.name = "Somebody"; p.identify(); p.moveAbout(); *** Person object referenced by Person variable: I'm acting as a person! My name is Somebody I'm movin' about! COMPSCI 230: OOD2
13
Inheritance example *** Student object referenced by Student variable:
System.out.println("*** Student object referenced by Student variable:"); Student s = new Student(); s.name = "Studiosus"; s.identify(); *** Student object referenced by Student variable: I'm acting as a university member! My name is Studiosus. Gaudeamus igitur! COMPSCI 230: OOD2
14
Inheritance example System.out.println("*** Lecturer object referenced by Lecturer variable:"); Lecturer l = new Lecturer(); l.name = "Professaurus"; l.identify(); l.assignToClass("COMPSCI230"); // no problem l.teach(); // also no problem l.moveAbout(); *** Lecturer object referenced by Lecturer variable: I'm acting as a university member! My name is Professaurus. Gaudeamus igitur! Teaching: COMPSCI230 I stride forward! COMPSCI 230: OOD2
15
Inheritance example *** Lecturer object referenced by Person variable:
System.out.println("*** Lecturer object referenced by Person variable:"); Person p2 = l; p2.identify(); // Person variable, but UniversityMember method() // p2.assignToClass("COMPSCI231"); // this won't compile // p2.teach(); // this won't compile either ((Lecturer) p2).assignToClass("COMPSCI280"); // explicit type cast ((Lecturer) p2).teach(); // explicit type cast *** Lecturer object referenced by Person variable: I'm acting as a university member! My name is Professaurus. Gaudeamus igitur! Teaching: COMPSCI230 COMPSCI280 COMPSCI 230: OOD2
16
Points to ponder 1 The UniversityMember class inherits all attributes and methods from Person So, a UniversityMember object also has a name, an identify() method, and a moveAbout() method Note that it also overrides the identify() method, i.e., replaces the identify() method with its own version UniversityMember doesn’t override the moveAbout() method, so it keeps the original version from Person UniversityMember adds an auid, and a protected attribute courses, which its subclasses (but not Person as a superclass) can also access Similarly, Lecturer and Student also inherit all attributes and methods from UniversityMember: name, auid, courses, identify(), moveAbout(). They override moveAbout() but they keep the identify() method from UniversityMember They also add their own methods (and could add their own fields COMPSCI 230: OOD2
17
Points to ponder 2 Every Student object is also a UniversityMember object (and so is every Lecturer object) Note: a Lecturer object is not a Student object, or vice versa Every UniversityMember object is also a Person object So: every Lecturer is a Person, and so is every Student Subclasses are simply more specialised types! We can store a reference to an object of a subclass in a variable declared as being of the type of the superclass E.g., can store a reference to a Student in a variable of type UniversityMember or Person We cannot do the opposite: a Student variable cannot reference any Person or UniversityMember object that is not also a Student COMPSCI 230: OOD2
18
Points to ponder 3 So, if we have a Person variable referencing an object that belongs to the Person class… …then that object could actually be a UniversityMember, Student, or Lecturer rather than just a plain vanilla Person The Person variable will only let us access the members that every Person object has: name, identify(), and moveAbout() E.g., we can’t invoke a method such as enrol(), study() or a field such as auid via an object variable declared as Person, even if the actual object that it references is a Student The method version that gets actually executed at runtime is that of the class of the actual object, however So, if we’re overriding a Person method in a subclass anywhere below Person, the overridden version in the subclass (or closest to it in the ancestry of the subclass) is the one that actually runs The ability to invoke different behaviours on a Person object based on the actual subclass of the instance is an example of polymorphism COMPSCI 230: OOD2
19
Polymorphism Different objects can respond differently to the same message. Inheritance is the “obvious” way to obtain polymorphic behaviour in your OO design, but it may not be the best way. Instantiations are polymorphic, if the values in their attributes affect the behaviour of their methods. Hmmm… if you have instantiated a million objects of a single Class, could they do anything useful? Hmmm…. Worker ants are (nearly) identical, but they won’t reproduce without a Queen ant. Ants may be important members of an ecosystem, but only if the ecosystem contains other forms of life, some inanimate objects, and an energy source. One way to conceive of OOD is that you’re designing an ecosystem with multiple species (Classes) in an evolutionary tree. It is possible to write a useful program in a non-OO language! Polymorphism is not necessary in programming, but it is fundamental to OO design. COMPSCI 230: OOD2
20
Explicit type casting Explicit type casting is found in a lot of programming languages, with C/C++ and Java probably being the most famous Explict type casting tells the compiler to treat the value in a variable of type X as if it was a value of type Y E.g., treat an int as a double: int i = 3; System.out.println(i/2); // 1 not 1.5 System.out.println((double) i/2); // type cast i to a double and get 1.5 In both C/C++ and Java, we type cast explicitly by putting the desired type in parentheses before the expression whose value we want to type cast Note that (type) is an operator and sits rather high in the order of operator precedence (e.g., above the division operator /) but below the object member access operator (“.”). Hence: ((Lecturer) p2).teach(); // works (Lecturer) p2.teach(); // doesn’t COMPSCI 230: OOD2
21
The meaning of super In a subclass, the keyword super refers to the superclass super() invokes the constructor of the superclass (we can also pass parameters if we wish) We can also use super to invoke methods that we have overridden: We’ll usually want to do this when the superclass method already does most of the job and we just want to add to it: public void identify() { super.identify(); System.out.print("I'm acting as a university member! "); System.out.print("My name is " + name + ". "); System.out.println("Gaudeamus igitur!"); } I'm acting as a person! My name is Studiosus I'm acting as a university member! My name is Studiosus. Gaudeamus igitur! COMPSCI 230: OOD2
22
Review questions How do you declare a subclass SubX of a class X in Java? Which members does a subclass have? If an object of a subclass is referenced by a variable x of type of its superclass, which methods and variables can you access via x? Under which circumstances can you reference an object of the type of the superclass in a variable that’s declared as being of the type of the subclass? When you override a method in a subclass, and you reference an object of that subclass in a variable x of type of its superclass, and you invoke the method on x, which version gets executed? How do you use an ArrayList? What do you need to do to be able to use it in your class? How do you use an Iterator? What does the visibility modifier protected mean? How do you access a method m() from its overridden version in a subclass? COMPSCI 230: OOD2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.