CSC9T4: Object Modelling, principles of OO design and implementation

Similar documents
CSCU9T4: Managing Information

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

INHERITANCE. Spring 2019

Inheritance and Polymorphism

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Object Oriented Programming. Java-Lecture 11 Polymorphism

CMSC 132: Object-Oriented Programming II

What is Inheritance?

Check out Polymorphism from SVN. Object & Polymorphism

Chapter 6 Introduction to Defining Classes

CS 251 Intermediate Programming Inheritance

CS-202 Introduction to Object Oriented Programming

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Java Magistère BFA

Concepts of Programming Languages

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS111: PROGRAMMING LANGUAGE II

CS111: PROGRAMMING LANGUAGE II

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

The major elements of the object-oriented model

Inheritance CSC 123 Fall 2018 Howard Rosenthal

Java Programming Lecture 7

ITI Introduction to Computing II

Lecture 5: Inheritance

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Rules and syntax for inheritance. The boring stuff

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

CMSC131. Library Classes

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

ITI Introduction to Computing II

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

Inheritance (Part 2) Notes Chapter 6

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

CPS 506 Comparative Programming Languages. Programming Language

COMP 250 Fall inheritance Nov. 17, 2017

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Making New instances of Classes

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Practice for Chapter 11

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

Everything is an object. Almost, but all objects are of type Object!

CS313D: ADVANCED PROGRAMMING LANGUAGE

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Java Fundamentals (II)

Inheritance. Transitivity

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. January 11, 2018

CS111: PROGRAMMING LANGUAGE II

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 14 Abstract Classes and Interfaces

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CMSC 132: Object-Oriented Programming II

Inheritance -- Introduction

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Java How to Program, 8/e

CS111: PROGRAMMING LANGUAGE II

Polymorphism. return a.doublevalue() + b.doublevalue();

Instance Members and Static Members

Inheritance and Polymorphism

CS1150 Principles of Computer Science Objects and Classes

The class Object. Lecture CS1122 Summer 2008

Inheritance (Part 5) Odds and ends

ECE 3574: Dynamic Polymorphism using Inheritance

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Outline. 15. Inheritance. Programming in Java. Computer Science Dept Va Tech August D Barnette, B Keller & P Schoenhoff

Computer Science 210: Data Structures

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Intro to Computer Science 2. Inheritance

Inheritance and Polymorphism

CISC370: Inheritance

CS/ENGRD 2110 SPRING 2018

Lecture 18 CSE11 Fall 2013 Inheritance

OBJECT ORİENTATİON ENCAPSULATİON

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Object-Oriented Programming Concepts

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

C++ Important Questions with Answers

Inheritance, Polymorphism, and Interfaces

Polymorphism and Inheritance

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Exam Duration: 2hrs and 30min Software Design

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

Inheritance (Outsource: )

PROGRAMMING LANGUAGE 2

Transcription:

CSC9T4: Object Modelling, principles of OO design and implementation CSCU9T4 Spring 2016 1 Class diagram vs executing program The class diagram shows us a static view of the responsibilities and relationships of the individual classes This helps us understand the Java program Classes exist in the program text. We compile the program text to produce an executable program that we can execute (run). The executing program comprises a collection of objects, actual instances of the classes, that interact by sending messages In general each class may give rise to any number of distinct objects At run-time, instances of classes are created using the keyword new. Objects exist in the running program. CSCU9T4 Spring 2016 2

Refine the object model Art Collection (Recall the purpose: a system to keep track of the art collection of the university: to allow the collection to be browsed to allow other galleries to borrow items ) What are the details of the main entities in the system (attributes, methods)? How do they interact? CSCU9T4 Spring 2016 3 Everything is a subclass of Object Every hierarchy needs a top element. Reminder: All classes implicitly extend the Object superclass Two important methods are inherited from Object : equals tostring equals: public boolean equals(object o) Called like this: if ( o1.equals(o2) ) Like o1 == o2 this checks whether the two references are identical (that is references to the same object) CSCU9T4 Spring 2016 4

It is often more useful to check whether the two objects have "equivalent contents" equals must be overridden Example for StaffRecord: Note: the cast public boolean equals(object o) { return name.equals((staffrecord)o.name); Note: the String equals method Then can call it like this: if ( sr1.equals(sr2) ) Note: sr1 == sr2 is meaningful but not always appropriate CSCU9T4 Spring 2016 5 tostring: public String tostring() Called like this: System.out.println( o.tostring() ); or, equivalently: System.out.println( o ); tostring returns: the name of the class of the object @ a hexadecimal hash code for the object (eg Student@33c0d9d) It is normal to override tostring to display something meaningful for our own classes Example for StaffRecord: public String tostring() { return name + " in room " + room; Note: + gives Then can call it like this: implicit tostring textfield.settext( sr1.tostring() ); System.out.println("Info: " + sr1); CSCU9T4 Spring 2016 6

Static versus Dynamic The picture so far of runtime is purely dynamic: Classes are instantiated to give objects which contain their own attribute data and (only implicitly) methods that use those attributes. A more detailed picture of this: sr1 sr2 Dynamically allocated objects name room name room............ Class file loaded into RAM (once) StaffRecord constructor getname tostring etc CSCU9T4 Spring 2016 7 Now the static aspects: We can also store variable/attribute data in the class RAM itself. These are called "class variables" (or attributes) and are not duplicated when a new object is allocated. All instance objects share the class variables. Declared with the keyword static. CSCU9T4 Spring 2016 8

Example in StaffRecord: private/public static int staffcounter = 0; Then in the constructor we could have: staffcounter++; id = staffcounter; a shared count of all instances, and unique ids! Can be referred to via objects: sr1.staffcounter sr2.staffcounter Or via the class name: StaffRecord.staffCounter even without any instances having been allocated! Note: class name, not object Can also have static methods can only refer to static variables, not dynamic (think about it!) Use with care! StaffRecord staffcounter... constructor getname tostring etc CSCU9T4 Spring 2016 9 Inheritance, Abstract, Interfaces and Multiple Inheritance CSCU9T4 Spring 2016 10

Inheritance & Abstract classes Sometimes, it does not make sense to have an instance of the superclass: The superclass is then being used only to define attributes and operations that are common to all its subclasses Such a superclass is called an abstract class We can indicate that Publication is to be an abstract class, i.e. one which has no instances and is therefore only there to be inherited from: public abstract class Publication {... Although, we cannot have (direct) instances (objects) of an abstract class such as Publication, we can have variables that can hold references to Publication objects: At run-time, the references will actually refer to objects of a subclass of Publication. CSCU9T4 Spring 2016 11 Inheritance & Abstract methods We can also have abstract methods or operations which have a heading, but no body For example the method borrow in class Publication is an abstract method A borrow method must then be defined in both the Book and Journal subclasses or in any further subclass which is itself to be non-abstract and instantiable A class must be abstract if it has one or more abstract methods But we can also require that a class is abstract even if none of its methods are abstract The use of final prevents a method from being overridden. Reference: docs.oracle.com/javase/tutorial/java/iandi/abstract.html CSCU9T4 Spring 2016 12

public abstract class Publication { protected int catnum; protected String title; public String gettitle() {... public int getcatnum() {... public abstract void borrow(member m); public void return() {... Note: no body public class Book extends Publication { private String author; public String getauthor() {... public void borrow(member m) {... public class Journal extends Publication { private int volnum; public int getvol() {... public void borrow(member m) {... CSCU9T4 Spring 2016 13 Interfaces Some programming languages support multiple inheritance where a subclass may have more than one superclass This is available in the implementation language C++ But not in Java it avoids various complications Java only has single inheritance But it also has interfaces Interfaces provide the advantages of multiple inheritance without the disadvantages. Try reading: http://docs.oracle.com/javase/tutorial/java/iandi/ createinterface.html CSCU9T4 Spring 2016 14

Interfaces & Abstract Classes An abstract class May have attributes And some of its operations may have implementations An interface is like a class But has no attributes (except final constants) And none of its operations have implementations An interface is therefore like a very abstract class It simply lists the public operations that an "extending" class must provide implementations for (but we use implements rather than extends) In effect it summarises a set of capabilities, a "contract" A class that offers actual methods for the public operations "promised" by an interface is said to implement that interface CSCU9T4 Spring 2016 15 Interfaces - Example Suppose that we have classes Rectangle and Balloon. They have various attributes and operations Suppose that we have a class Mover whose purpose is to move objects around by calling operations left and up Mover can deal with any object that offers the operations left and up via the interface Moveable We can represent an interface in a UML diagram in a similar way to a class, but there are only two partitions (no attributes) and we use the stereotype <<interface>>. CSCU9T4 Spring 2016 16

Interfaces - Example Here we have defined an interface called Moveable We show that class Mover depends on the interface Moveable by a dashed arrow from Mover to Moveable We show the relationship between Moveable and the classes Rectangle and Balloon by using a dashed (weaker) form of inheritance. We say that Rectangle and Balloon realise the Moveable interface. CSCU9T4 Spring 2016 17 Interfaces - Example An object of class Mover can call the operations left and up offered by a Moveable object. What do we mean by a Moveable object? The answer is an object such as Rectangle and Balloon that realises the Moveable interface. By realising the Moveable interface, both Rectangle and Balloon must provide implementations for the operations left and up. In Java, we say that Rectangle and Balloon implements the interface Moveable. CSCU9T4 Spring 2016 18

Interfaces - Example What is going on might become clearer if we look at some Java: public interface Moveable { public void left(int d); public void up(int d); public class Rectangle implements Moveable {... left... up... (full definitions)... size... grow... (Rectangle specific items)... public class Balloon implements Moveable {... left... up... (full definitions)... expand... (Balloon specific items)... CSCU9T4 Spring 2016 19 And we could have a very general class Mover that has a reference to a Moveable as an attribute, and a main program that uses it: class Mover { Moveable m; public Mover(Moveable m) { this.m = m; private void moveit() { m.left(17); m.up(25); Main program: mr = new Mover( new Rectangle(...)); mb = new Mover( new Balloon(...)); mr.moveit(); mb.moveit(); m can refer to any object that implements the Moveable interface However, the only methods that can be called are those offered by Moveable So, even when m is pointing at a Rectangle object, it cannot call the Rectangle operations size and grow CSCU9T4 Spring 2016 20

Interfaces - Example An interface Moveable specifies a contract, the classes Rectangle and Balloon guarantee to carry out the contract. Objects of class Mover deal in terms of Moveable references, they have no need to know what kind of class has implemented the interface. Pre- and post-conditions can be attached to an operation in an interface to specify what the implementation of the operation must achieve. One advantage of interfaces is that we have shown that class Mover only depends on the Rectangle and Balloon operations that are in the Moveable interface. If Rectangle or Balloon were modified so that one of their other operations changed, we have a guarantee that Mover would not be affected. CSCU9T4 Spring 2016 21 Why interfaces are useful Interfaces are useful as they allow our designs/programs to be more general/flexible than they otherwise would be Natural choices for interface names are adjectives For example: We can use an interface name, say Moveable, as the type of a formal parameter: private void mymethod(moveable m,...) {... This indicates that mymethod is happy to receive any object at all as an actual parameter provided that it offers the operations specified in Moveable The actual parameter can be an instance of any class that "implements Moveable" (and the Java compiler checks for us!) CSCU9T4 Spring 2016 22

Interfaces & Roles A use of interfaces is that they allow a class to play different roles. An object of a Person class may play the role of Employee. The operations used in that role may be defined in an Employable interface. An Employer object then deals with Person objects through their Employable interface rather than directly manipulating Person objects. As well as the operations shown in the Employable interface, Person objects may have operations they use when interacting with their children. These operations could be defined in a Responsible interface. CSCU9T4 Spring 2016 23 Multiple Interfaces Many classes can realise a given interface and a given class can realise several alternative interfaces. So that the UML diagram does not become too cluttered, we can represent interfaces in the following way: The interface is collapsed into a "lollipop'' showing that Rectangle and Balloon implement the interface Moveable. The dashed arrow shows that Mover depends on the interface Moveable. CSCU9T4 Spring 2016 24

The Collections Framework and Collection Interface CSCU9T4 Spring 2016 25 Why use Inheritance? Discussion CSCU9T4 Spring 2016 26

Advantages of OOP 1. Software reuse is enhanced. 2. Software maintenance cost can be reduced. 3. Data access is restricted providing better data security. 4. Software is easily developed for complex problems. 5. Software may be developed meeting the requirements on time, on the estimated budget. 6. Software has improved performance. 7. Software quality is improved. 8. Class hierarchies are helpful in the design process allowing increased extensibility. 9. Modularity is achieved. 10. Data abstraction is possible. CSCU9T4 Spring 2016 27 End of lecture CSCU9T4 Spring 2016 28