Lecture 6: Object-Oriented Design, Part 3

Slides:



Advertisements
Similar presentations
Object-Oriented Design – Key Concepts Version 1.1 of : added learning objectives CompSci 230 Software Construction.
Advertisements

METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
ITEC200 – Week03 Inheritance and Class Hierarchies.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Lecture 5: Object-Oriented Design, Part 2 CompSci 230 Software Construction.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Programming in Java CSCI-2220 Object Oriented Programming.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 8 Inheritance. 2  Review of class relationships  Uses – One class uses the services of another class, either by making objects of that class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
CompSci 230 Software Construction
Object-Oriented Design
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Overriding Method.
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
CompSci 230 Software Construction
Continuing Chapter 11 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Lecture 23 Polymorphism Richard Gesick.
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Extending Classes.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Java Programming
METHOD OVERRIDING in JAVA
Java – Inheritance.
Inheritance Cse 3rd year.
Java Inheritance.
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
Java Programming Language
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 11 Inheritance and Polymorphism Part 1
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Lecture 6: Object-Oriented Design, Part 3 CompSci 230 Software Construction Lecture 6: Object-Oriented Design, Part 3

Agenda & Reading Topics: Reading Inheritance, instantiation, and polymorphism Reading The Java Tutorials, on Inheritance Wikipedia, on Class Diagram COMPSCI 230: OOD2

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

Inheritance example Person UniversityMember Lecturer Student COMPSCI 230: OOD2

(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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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