SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Java Implementation: Part 3 Software Construction Lecture 8.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
JAVA COURSE 1 Computer Engineering Association. Compile your first program Public class Hello{ public class Hello(){ System.out.println(“Hello”); } puclic.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Design issues for Object-Oriented Languages
Object Lifetime and Pointers
Modern Programming Tools And Techniques-I
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
CompSci 230 S Programming Techniques
Lecture 4: Type Systems.
CSE 116/504 – Intro. To Computer Science for Majors II
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Programming Language Concepts (CIS 635)
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Continuing Chapter 11 Inheritance and Polymorphism
Object Oriented Programming
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Lecture 23 Polymorphism Richard Gesick.
Lecture 3: Extending Classes Inheritance in Java
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Inheritance Basics Programming with Inheritance
More inheritance, Abstract Classes and Interfaces
Extending Classes.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Lecture 4: Extending Classes
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Computer Programming with JAVA
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Session 2: Introduction to Object Oriented Programming
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Inheritance in Java CS 3331 Fall 2009.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Java Programming Language
Data Structures & Algorithms
Java Basics Data Types in Java.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
In this class, we will cover:
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Agenda Inheritance Polymorphism Type compatibility Homework.
Classes and Objects Object Creation
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Presentation transcript:

SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin

Agenda – Lecture 5a Homework (4b) Responsibilities, an introduction Quiz 1 review, … OO Objects and variables. Inheritance, subtyping. 2/4/2019 SOEN 343, © P.Chalin,

Homework was … Planned (e.g. MDD) vs. Evolutionary (e.g. XP) How does each approach address: No doc. produced. Increased gap: design vs. implementation. Add-on syndrome. 2/4/2019 SOEN 343, © P.Chalin,

Responsibility Assignment GRASP – Larman. Responsibility-Drive Design (RDD) – Wirfs-Brock et. al. 2/4/2019 SOEN 343, © P.Chalin,

General Classification of Kinds of Responsibility To know. To do. To decide. 2/4/2019 SOEN 343, © P.Chalin,

Responsibilities – A Boat Metaphor What kind of responsibilities do each of the following “objects” have: … To know. To do. To decide. 2/4/2019 SOEN 343, © P.Chalin,

Responsibilities – A Boat Metaphor Kind of responsibility for: Captain To know? To do? To decide? 2/4/2019 SOEN 343, © P.Chalin,

Responsibilities – A Boat Metaphor Kind of responsibility for: Navigator. To know? To do? To decide? 2/4/2019 SOEN 343, © P.Chalin,

Responsibilities – A Boat Metaphor Kind of responsibility for: Compass. To know? To do? To decide? 2/4/2019 SOEN 343, © P.Chalin,

Quiz 1 Focus OO fundamentals. Basic UML. Basic GRASP (applied). 2/4/2019 SOEN 343, © P.Chalin,

Quiz 1 Review You will be given explanation, not necessarily answers. If you need further clarification: My office hours. Ask during tutorial. 2/4/2019 SOEN 343, © P.Chalin,

Quiz 1, Qn 1: Objects and Variables Some ‘theory’ … 2/4/2019 SOEN 343, © P.Chalin,

Object and Variables: Run-time Initialization Local variables: Are not implicitly initialized. Must be explicitly initialized. Object fields always initialized by default to Integral types (including char): 0. Floating point types: 0.0. Reference types: null. 2/4/2019 SOEN 343, © P.Chalin,

Object Creation and Variable Declarations Basic declaration (no object creation): Animal a; null initialized declaration (no object creation): Animal a = null; Only using new will create (instantiate) objects a = new Duck(); 2/4/2019 SOEN 343, © P.Chalin,

Objects and Variables: Key Points What you should know: Run-time memory model Has two parts: Stack, Heap. You should know what gets allocated where. Object creation and variable declarations. Declarations do not cause objects to be created. Know the role of null. Object creation / instantiation: via new. 2/4/2019 SOEN 343, © P.Chalin,

Stack and Heap: Quiz Question Given the following declarations int i = 2; int[] b = new int[2]; String r; String s = “abc”; String t = null; What will be the state of the stack and heap after they have all been processed? 2/4/2019 SOEN 343, © P.Chalin,

Stack and Heap Stack Heap i 2 b r ? [0,0] s t null “abc” 2/4/2019 SOEN 343, © P.Chalin,

Quiz 1, Qn 2: Type Hierarchy 2/4/2019 SOEN 343, © P.Chalin,

Disjointedness of Primitive and Reference Types You cannot assign a primitive type to a variable of a reference type. You cannot assign a reference to a variable of a primitive type. The remaining few slides discuss only reference types. 2/4/2019 SOEN 343, © P.Chalin,

Type Hierarchy (Lect. 1b) Every class is a subclass of Object. If S is a subclass of T then we can use an instance of S where ever a T is expected: T t = new S(); Object o = new S(); // Do not do this (it is wasteful): Object o = new String(“abc”); 2/4/2019 SOEN 343, © P.Chalin,

Each Reference Variable: Two Types Each variable of a reference type has Declared type – fixed. Run-time type – can change at run-time. Synonyms: Declared type: apparent type. Run-time type: dynamic, or actual type. 2/4/2019 SOEN 343, © P.Chalin,

Each Variable: Two Types, Example Object m = new Movie(); m has two types: Declared type: Object. Run-time type: Movie. 2/4/2019 SOEN 343, © P.Chalin,

Type Hierarchy: Object, the root of all K B X 2/4/2019 SOEN 343, © P.Chalin,

Type Hierarchy: Supertypes, Subtypes Supertypes of A Object Subtypes of A A K B X 2/4/2019 SOEN 343, © P.Chalin,

Unrelated Sub-hierarchies are Not Compatible (for assignment, cast). Object Cannot Assign, or Cast A to K A B X K 2/4/2019 SOEN 343, © P.Chalin,

‘Declared Type’ Fixes Bounds Declared type – fixed, e.g. A. Run-time type – can change at run-time ... … within bounds of declared type subhierarchy. A B X 2/4/2019 SOEN 343, © P.Chalin,

Type Checking: Assignment Always done relative to declared type. A a = some-expression-of-type-X Legal? (or will cause a compile-time error?) Assignment is legal iff X is A, Subtype of A. A B X 2/4/2019 SOEN 343, © P.Chalin,

Type Casting and Type Checking I A a = (X) some-expression-of-type-K; Legal? (will cause a compile-time error?) Same answer as given on previous slide because this is just a special case of the previous slide. 2/4/2019 SOEN 343, © P.Chalin,

Type Casting and Type Checking II A a = (X) some-expression-of-type-K; Legal? (will cause a compile-time error?) Error unless X and K are related (super- or sub-type). X … Z K 2/4/2019 SOEN 343, © P.Chalin,

Type Casting and Run-time Checks? I A a = (X) some-expression-of-type-K; If X and K are related then: X is a supertype of K: nothing is needed. X … K 2/4/2019 SOEN 343, © P.Chalin,

Type Casting and Run-time Checks? II A a = (X) some-expression-of-type-K; If X and K are related then: X is a subtype of K: a run-time check is needed. K … X 2/4/2019 SOEN 343, © P.Chalin,

Question 2 (a)-(c), Another Try Find the answer (but also tell me why): (a) Object o = 1; C / R / N (b) Object o = s; C / R / N (c) Object o = (Object) s; C / R / N 2/4/2019 SOEN 343, © P.Chalin,

Question 2 (d)-(f), Another Try String t = x; C / R / N String t = (String)x; C / R / N String t = (Object)s; C / R / N 2/4/2019 SOEN 343, © P.Chalin,

Question 3 a : A c : C b : B 1 : m1( ) 2 : m2 ( ) c 2/4/2019 SOEN 343, © P.Chalin,

Question 3, Code – possible answer class A { B b; public void m() { C c = b.m1(); c.m2(); } class B { C c; ... public C m1() { ...; return c; } class C { public void m2() { ... } 2/4/2019 SOEN 343, © P.Chalin,

Question 4 Variant of Exercise Set 1, Question 3. 2/4/2019 SOEN 343, © P.Chalin,

Dynamic Dispatching … Run-time type is only of concern when resolving non-static methods. Never for fields. Not for static methods. 2/4/2019 SOEN 343, © P.Chalin,

Overridden Non-static Field There are two fields (one in P, one in C). Moral: do not do this! P f : int C f : int 2/4/2019 SOEN 343, © P.Chalin,

Static Methods No overriding P.m(), C.m() are distinct methods, both accessible. P int m() C int m() 2/4/2019 SOEN 343, © P.Chalin,

Question 5, GRASP: Polymorphism, … Solution: Exercise Set 3 Tutorial 3 2/4/2019 SOEN 343, © P.Chalin,

Computing Movie Charge 2/4/2019 SOEN 343, © P.Chalin,