INF3110: Exercises Part 6 Object Orientation Comments and solutions

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
INF5110: Mandatory Exercise 2 Eyvind W. Axelsen @eyvindwa Slides are partly based on.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
6-1 © 2004, D.A. Watt, University of Glasgow 6 Data Abstraction  Packages and encapsulation.  Abstract types.  Classes, subclasses, and inheritance.
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Programming in Java CSCI-2220 Object Oriented Programming.
Types in programming languages1 What are types, and why do we need them?
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Do-it-yourself dictionaries in Haskell1 Ingmar Brouns & Lukas Spee.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Topic: Classes and Objects
Polymorphism.
Inheritance ITI1121 Nour El Kadri.
Polymorphism 2nd Lecture
Chapter 11 Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Programming Language Concepts (CIS 635)
12 Data abstraction Packages and encapsulation
INF3110: Exercises Part 3 Comments and Solutions
Eyvind W. Axelsen INF3110: Exercises Part 7 Types and Object Orientation II Comments and solutions Eyvind W. Axelsen
IST311 Advanced Issues in OOP: Inheritance and Polymorphism
Continuing Chapter 11 Inheritance and Polymorphism
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Compiler Design 18. Object Oriented Semantic Analysis (Symbol Tables, Type Checking) Kanat Bolazar March 30, 2010.
Polymorphism 2nd Lecture
Java Programming Language
Polymorphism and access control
Chapter 9: Polymorphism and Inheritance
Week 6 Object-Oriented Programming (2): Polymorphism
Generic programming in Java
Eyvind W. Axelsen INF3110: Solutions to Problem Set 2 Scoping, Runtime Organisation, Parameter Passing Methods Eyvind W. Axelsen.
COMPUTER 2430 Object Oriented Programming and Data Structures I
INF3110: Exercises Part 6 Object Orientation Comments and solutions
Review of Previous Lesson
Chapter 11 Inheritance and Polymorphism Part 2
Review of Previous Lesson
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
CS 240 – Advanced Programming Concepts
Presentation transcript:

INF3110: Exercises Part 6 Object Orientation Comments and solutions Eyvind W. Axelsen eyvinda@ifi.uio.no @eyvindwa http://eyvinda.at.ifi.uio.no Slides are based on material from previous years, made by Weiqing Zhang and Henning Berg.

Structural typing : type compatibility and equivalence are determined by the type's actual structure or definition Nominal typing: which compatibility and equivalence of data types is determined by explicit declarations and/or the name of the types

Problem 1 Which of these four statements are correct under a) name compatibility? b) structural compatibility?

Problem 1 a) Incorrect Correct Which of these four statements are correct under a) name compatibility? b) structural compatibility?

Problem 1 b) Correct Incorrect Which of these four statements are correct under a) name compatibility? b) structural compatibility?

Subtyping If S is a subtype of T, the subtyping relation is often written S <: T, to mean that any term of type S can be safely used in a context where a term of type T is expected Integer <: Number Java: String <: Object Next problem : Cheese <: Food

Subtyping If T1 → T2 is a function type, then a subtype of it is any function  with the property that T1 <: S1 and S2 <: T2 S1 → S2 <: T1 → T2 Argument(s): contravariant Return type: covariant Object -> ArrayList <: ArrayList -> Object

Problem 2 class Food { … } class Cheese extends Food { … } int f (Cheese c) { … } int f’ (Food f) { .. } // in this language, f’ is just an ordinary name

Problem 2 The answer is of course that f expects a more specific type than Food, namely Cheese. Cheese might have properties that Food does not. For instance, imagine this definition of Cheese: class Cheese extends Food { void melt() { … } } and this definition of f: int f (Cheese c) { … c.melt(); … } Clearly, a call to f(someFood) would not work here.

Problem 3 – Exercise 10.2 in Mitchell Switch on type tag Type cast a) Rewrite this so that each class has a Rotate method, and no tag field (in other words, make an OO solution)

Problem 3 - 10.2 a) class Point { int x, y; void move(int dx, int dy){ x +=dx; y +=dy }; void rotate(){}; };  class Circle extends Point { // the inherited point can be the center int radius; // no new move() or rotate() methods are needed };  class Rectangle extends Point { // the inherited point can be the center, although the original // did not have that Point topLeft, bottomRight; void rotate(){ /* implement rotate here */ }; };

Problem 3 - 10.2 a) class Point { int x, y; void move(int dx, int dy){ x +=dx; y +=dy }; void rotate(){}; };  class Circle extends Point { // the inherited point can be the center int radius; // no new move() or rotate() methods are needed };  class Rectangle extends Point { // the inherited point can be the center, although the original // did not have that Point topLeft, bottomRight; void rotate(){ /* implement rotate here */ }; }; Another option would be to add a Shape class at the top of the hierarchy, and letting Point, Circle and Rectangle inherit from this class.

Problem 3 - 10.2 b) What if we add a Triangle class? What modifications would be necessary with the original version, and our new version?

Problem 3 - 10.2 b) What if we add a Triangle class? What modifications would be necessary with the original version, and our new version? Original: modify the shape tag enum to include a triangle tag add a new triangle class change the rotate procedure

Problem 3 - 10.2 b) What if we add a Triangle class? What modifications would be necessary with the original version, and our new version? New, OO, version: add a new triangle class (with required methods) class Rectangle extends Point { Point p1, p2, p3 // the three points defining the triangle void rotate(){ /* implement rotate here */ }; void move(){ /* implement move here */ }; };

Problem 3 – 10.2 c) Discuss the differences between changing the definition of the rotate method in the original and new (OO) version. (Remember that we have added the Triangle.) Both versions would require invasive changes The Mitchell only to one procedure (the common rotate), while our new solution would require changes to all non-trivial rotate methods.

Problem 4 a) Which of the methods C_equals 1 or SC_equals 1 will be called by the statements below? Remember that which overload to call is determined at compile-time, while which override to call is determined at runtime. c.equals(c) c.equals(c’) c.equals(sc) c’.equals(c) c’.equals(c’) c’.equals(sc) sc.equals(c) sc.equals(c’) sc.equals(sc) C’ 和 C是相同的 subtype表示实质的概念级的继承,往往符合is-a关系 subclass表示结构上的继承(的确是为了复用),不一定符合is-a关系 从这个角度区分subclass和subtype可以得到同样的结果: 父类的任何子类都可以在运行时可无缝的代替父类的工作,那么是subtype(因为符合了概念上的is-a)

Problem 4 a) Which of the methods C_equals 1 or SC_equals 1 will be called by the statements below? Remember that which overload to call is determined at compile-time, while which override to call is determined at runtime. c.equals(c) c.equals(c’) c.equals(sc) c’.equals(c) c’.equals(c’) c’.equals(sc) sc.equals(c) sc.equals(c’) sc.equals(sc) C_equals 1 SC_equals 1 SC_equals1 equals 2

Problem 4 b) Suppose that SC_equals 1 is no longer there. Remember that which overload to call is determined at compile-time, while which override to call is determined at runtime. c.equals(c) c.equals(c’) c.equals(sc) c’.equals(c) c’.equals(c’) c’.equals(sc) sc.equals(c) sc.equals(c’) sc.equals(sc) C’ 和 C是相同的 subtype表示实质的概念级的继承,往往符合is-a关系 subclass表示结构上的继承(的确是为了复用),不一定符合is-a关系 从这个角度区分subclass和subtype可以得到同样的结果: 父类的任何子类都可以在运行时可无缝的代替父类的工作,那么是subtype(因为符合了概念上的is-a)

Problem 4 b) Suppose that SC_equals 1 is no longer there. Remember that which overload to call is determined at compile-time, while which override to call is determined at runtime. c.equals(c) c.equals(c’) c.equals(sc) c’.equals(c) c’.equals(c’) c’.equals(sc) sc.equals(c) sc.equals(c’) sc.equals(sc) C_equals 1 C_equals1 equals 2 C’ 和 C是相同的 subtype表示实质的概念级的继承,往往符合is-a关系 subclass表示结构上的继承(的确是为了复用),不一定符合is-a关系 从这个角度区分subclass和subtype可以得到同样的结果: 父类的任何子类都可以在运行时可无缝的代替父类的工作,那么是subtype(因为符合了概念上的is-a)

Problem 5 a) class Date { int year, month, day; Write in Java an abstract data type and a class for a data type Date, with year, month and day, and operations before, after and daysBetween. Abstract data type: meaning of an operation is always the same operation (operands) Class: Meaning of operation might depend on runtime type of object (polymorphism/dynamic dispatch) object.operation(arguments) Abstract data type: class Date { int year, month, day; Date date(int y, m, d) {.. ; return new Date(...) ; ..} boolean static before(Date d1,Date d2) { if (d1.year < d2.year) {return true} else if (d1.year > d2.year) {return false} else if (d1.month < d2.month) {return true} else if (d1.month > d2.month) {return false} else return d1.day < d2.day; }; .. }

Problem 5 a) class Date { int year, month, day; Write in Java an abstract data type and a class for a data type Date, with year, month and day, and operations before, after and daysBetween. Abstract data type: meaning of an operation is always the same operation (operands) Class: Meaning of operation might depend on runtime type of object (polymorphism/dynamic dispatch) object.operation(arguments) Class class Date { int year, month, day; Date date(int y, m, d) {.. ; return new Date(...) ; ..} boolean before(Date d) { if (year < d.year) {return true} else if (year > d.year) {return false} else if (month < d.month) {return true} else if (month > d.month) {return false} else return day < d.day; }; .. }

Problem 5 b) class Month { int mAsInt; Boolean before(Month m) { return mAsInt < m.mAsInt; } }; class Year {/* correspondingly */}; class Day {/* correspondingly */};   class Date { Year year; Month month, Day day; Date date(Year y, Month m, Day d) {..} boolean before(Date d) { if year.before(d.year) {return true} else if month.before(d.month) {return true} else return day.before(d.day); } How would you make the Date class independent of the representation of years, months and days? Abstraction is key!

Some extra material to cure your Game of Thrones withdrawal syndromes (probably not) Types and Programming Languages Benjamin C. Pierce Pretty accessible/good introduction Type theory Lambda calculus Available at the Ifi library https://eev.ee/blog/2016/12/01/lets-stop-copying-c/ Let’s stop copying C Blog post about syntax, semantics and some type stuff regarding. Comparing C with new languages