CSCU9T4: Object Modelling, principles of OO design and implementation

Similar documents
CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

History OOP languages Year Language 1967 Simula Smalltalk

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Object Oriented Design

Java Interview Questions and Answers

Java Programming Language

Chapter 1 Fundamentals of Java Programming

Description of Class Mutation Mutation Operators for Java

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

3 Pillars of Object-oriented Programming. Industrial Programming Systems Programming & Scripting. Extending the Example.

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Java (12 Weeks) Introduction to Java Programming Language

Fundamentals of Java Programming

Building Java Programs

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Java Application Developer Certificate Program Competencies

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

ATM Case Study OBJECTIVES Pearson Education, Inc. All rights reserved Pearson Education, Inc. All rights reserved.

Case studies: Outline. Requirement Engineering. Case Study: Automated Banking System. UML and Case Studies ITNP090 - Object Oriented Software Design

Gadget: A Tool for Extracting the Dynamic Structure of Java Programs

Glossary of Object Oriented Terms

Agile Software Development

Assignment # 2: Design Patterns and GUIs

Formal Engineering for Industrial Software Development

Object-Oriented Programming: Polymorphism

UML Class Diagrams (1.8.7) 9/2/2009

Abstract Class & Java Interface

Java CPD (I) Frans Coenen Department of Computer Science

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Inheritance, overloading and overriding

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

Specialized Programme on Web Application Development using Open Source Tools

CS193j, Stanford Handout #10 OOP 3

Advanced Data Structures

Java Classes. GEEN163 Introduction to Computer Programming

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Programming Languages Featherweight Java David Walker

Java Software Structures

Chapter 13 - Inheritance

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Compile-time type versus run-time type. Consider the parameter to this function:

How Scala Improved Our Java

An Introduction to Object-Oriented Programming with

CS 111 Classes I 1. Software Organization View to this point:

Embedded Software development Process and Tools: Lesson-1

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

The Basic Java Applet and JApplet

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

UML for C# Modeling Basics

Construction of classes with classes

Konzepte objektorientierter Programmierung

CIS 190: C/C++ Programming. Polymorphism

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

Programmation 2. Introduction à la programmation Java

D06 PROGRAMMING with JAVA

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

PLV Goldstein 315, Tuesdays and Thursdays, 6:00PM-7:50PM. Tuesdays and Thursdays, 4:00PM-5:30PM and 7:50PM 9:30PM at PLV G320

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Android Application Development Course Program

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

AP Computer Science Java Subset

Java EE Web Development Course Program

Applying Object-Oriented Principles to the Analysis and Design of Learning Objects

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

The Interface Concept

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

1. Polymorphism in C++...2

ICOM 4015: Advanced Programming

Introduction to Object-Oriented Programming

Syllabus for CS 134 Java Programming

C++ INTERVIEW QUESTIONS

An Introduction To UML Class Diagrams Classes

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Génie Logiciel et Gestion de Projets. Object-Oriented Programming An introduction to Java

Specialized Programme on Web Application Development using Open Source Tools

Part 3: GridWorld Classes and Interfaces

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

Part I. Multiple Choice Questions (2 points each):

Computing Concepts with Java Essentials

Using Inheritance and Polymorphism

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

How To Design Software

UML Class Diagrams. Lesson Objectives

5. Advanced Object-Oriented Programming Language-Oriented Programming

Lab Manual: Using Rational Rose

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

Abstract. a

PHP Code Design. The data structure of a relational database can be represented with a Data Model diagram, also called an Entity-Relation diagram.

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

Transcription:

CSCU9T4: Object Modelling, principles of OO design and implementation CSCU9T4 Spring 2016 1 What is Inheritance? Why use Inheritance? Discussion CSCU9T4 Spring 2016 2

Inheritance In inheritance, a subclass "extends" the definition of its superclass It implicitly includes the attributes and operations from the superclass, and may add attributes and operations and may redefine the implementations of operations For example, inheritance is shown in the following, where classes Book and Journal "inherit from" Publication: Note: UML notation. Arrowheads can be closed: inheritance open: reference (via an attribute) CSCU9T4 Spring 2016 3 Inheritance - extends public class Publication{... public class Book extends Publication {... public class Journal extends Publication {... We can have an inheritance hierarchy If, say, Publication is itself a subclass of an even higher-level class Or, say, Book has subclasses CSCU9T4 Spring 2016 4

public abstract class Publication { protected int catnum; protected String title; // operations public class Book extends Publication { private String author; // operations public class Journal extends Publication { private int volnum; // operations CSCU9T4 Spring 2016 5 Inheritance & Attributes Although we want to hide the attributes of a superclass from ordinary clients, we usually want to make them visible in the subclass This is not automatic with private attributes Hence, we there are three levels of visibility: public, private and protected. public : visible to clients private : visible only within the class protected : visible only within the class and its subclasses So that the method bodies in Book and Journal can refer to title and catnum: protected int catnum; protected String title; CSCU9T4 Spring 2016 6

Object Modelling Art Collection Revisit the art collection example: Consider a system to keep track of the art collection of the university. This might have a number of purposes: to allow the collection to be browsed to allow other galleries to borrow items What are the main entities in the system? How are they related? CSCU9T4 Spring 2016 7 Executing program and GUI The JVM launches the program by calling the main method This typically creates an instance of the Boundary class Boundary app = new Boundary(); Typically the Boundary constructor then builds a GUI (JFrame) The interactions between the user and the program are represented in Boundary as a set of operations. What are these for the Art Collection? In a GUI, each operation can be implemented by a button and the parameters by information in text fields. What are these for the Art Collection? CSCU9T4 Spring 2016 8

What s the highest object in the hierarchy? (How do we use a gallery/catalog/?) What operations are provided to the user (via actionperformed)? Trace the program: what happens when a button is pressed? what happens when we create a collection object? (what operation is called, what information is needed?) what happens when we create an artwork object? (what operation is called, what information is needed?) CSCU9T4 Spring 2016 9 Inheritance & Operations CSCU9T4 Spring 2016 10

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() {... 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 11 Inheritance & Operations Note that the operation borrow is given in all three classes Publication, Book and Journal That shows that it is defined in Publication... And the definition is overridden in the Book and Journal subclasses This is useful if we wish to indicate: That all Publications, whether they are a book or a journal, have a public borrow operation But that the actual details for borrowing a Book and borrowing a Journal are different so the two subclasses must have separately defined methods We also need to decide whether we need an actual implementation (method body) of borrow in Publication There does not need to be, and in this example there is not indicated by the keyword abstract (See discussion of "abstract methods" later on) CSCU9T4 Spring 2016 12

Instantiation of subclasses To be completely clear: When a subclass is instantiated using new: Enough storage is allocated for all the attributes of the subclass and its one (or more) superclasses Effectively, all the subclass's operations are included, plus all the non-overridden operations from the superclasses except the constructors A subclass constructor may call its superclass's constructor using super( );» (This is Java; details may vary slightly for other OO languages) Therefore: An instance of a subclass has all the properties expected of an instance of a superclass For example: a Book has title, gettitle, catnum, return, borrow, etc CSCU9T4 Spring 2016 13 A consequence of the previous slide, and a feature of inheritance in object-oriented systems is that: Anywhere a reference to a superclass object is expected, we can use a reference to a subclass object instead because we can guarantee that it has all the capabilities Hence, a reference to a Publication object could refer to a Book object Consider some examples: are these valid? What do they do? Publication pub = new Publication("Smith"); Book pub = new Book("Smith"); Publication pub = new Book( Smith"); Book pub = new Publication("Smith"); CSCU9T4 Spring 2016 14

Further: When we call an operation through a reference, the actual method used depends on the actual object referred to (Again, this is Java, and details may vary in other OO languages) For example, in Java, after the declaration: Publication pub = new Book( Smith"); What happens if we call the borrow operation: pub.borrow(...); What if we added pub = new Journal( LNCS"); Now what happens if we call pub.borrow(); CSCU9T4 Spring 2016 15 Overriding is not Overloading Any class may have more than one method with the same name, but we have overloading when: The formal parameters lists are distinguishable This allows the compiler/jvm to determine which actual method is being called. E.g. JButton b1 = new JButton(); JButton b2 = new JButton("Press me"); If Book has, say: public void setdetails(string t) {... public void setdetails(string t, String a) {... then book1.setdetails( The Oxford English Dictionary"); book1.setdetails( The Wonderbox", Roman Krznaric"); CSCU9T4 Spring 2016 16

This is polymorphism and dynamic binding Try reading: http://docs.oracle.com/javase/ tutorial/java/iandi/polymorphism.html CSCU9T4 Spring 2016 17 Art Collection and Inheritance Revisit the art collection example: Are there operations occurring in superclasses and subclasses here? Which operations are the same in all? And which operations are different? CSCU9T4 Spring 2016 18

Why use Inheritance? Discussion CSCU9T4 Spring 2016 19 End of lecture CSCU9T4 Spring 2016 20