Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 2017

Similar documents
Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 2017

Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 4, 2016

Last name:... First name:... Department (if not D-INFK):...

Exam Duration: 2hrs and 30min Software Design

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

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

JAVA OPERATORS GENERAL

University of Palestine. Mid Exam Total Grade: 100

INSTRUCTIONS TO CANDIDATES

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

ECE 461 Fall 2011, Final Exam

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

JAVA Programming Language Homework I - Nested Class

Instance Members and Static Members

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Overview. Elements of Programming Languages. Objects. Self-Reference

Inheritance (Part 2) Notes Chapter 6

Practice for Chapter 11

Exercise: Singleton 1

First IS-A Relationship: Inheritance

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

More Relationships Between Classes

Inheritance (Part 5) Odds and ends

JAVA. Lab-9 : Inheritance

Exercise 3 Subtyping and Behavioral Subtyping October 13, 2017

Overview. Elements of Programming Languages. Objects. Self-Reference

Last name:... First name:... Department (if not D-INFK):...

CSE 130, Fall 2006: Final Examination

Java Simple Data Types

Exercise 9 Parametric Polymorphism and Information Hiding November 24, 2017

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Prelim 1. CS 2110, 13 March 2018, 5:30 PM Total Question Name Short answer

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Final Exam Practice Questions

CS 201, Fall 2016 Sep 28th Exam 1

Java Simple Data Types

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

PROGRAMMING LANGUAGE 2

H212 Introduction to Software Systems Honors

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Example: Count of Points

CIS 120 Midterm II November 8, 2013 SOLUTIONS

CS 61B Discussion 5: Inheritance II Fall 2014

The Java language has a wide variety of modifiers, including the following:

Chapter 8 ( ) Control Abstraction. Subprograms Issues related to subprograms How is control transferred to & from the subprogram?

FAQ: Classes & Objects

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Data Abstraction. Hwansoo Han

What is Inheritance?

Nested Classes in Java. Slides by: Alon Mishne Edited by: Eran Gilad, Eyal Moscovici April 2013

Chapter 12: Inheritance

Software and Programming 1

Java Object Oriented Design. CSC207 Fall 2014

Class, Variable, Constructor, Object, Method Questions

CS 231 Data Structures and Algorithms, Fall 2016

Programming in C++: Assignment Week 6

Advanced Systems Programming

CSCI-GA Final Exam

Conversions and Overloading : Overloading

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

G Programming Languages - Fall 2012

Selenium Class 9 - Java Operators

Records. ADTs. Objects as Records. Objects as ADTs. Objects CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 15: Objects 25 Feb 05

CIS 120 Final Exam 15 December 2017

Key Java Simple Data Types

Software and Programming 1

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

JAVA Programming Language Homework II Student ID: Name:

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

CMSC 132: Object-Oriented Programming II

CSC 240 Computer Science III Spring 2018 Midterm Exam. Name

Exercise 8 Parametric polymorphism November 18, 2016

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

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

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Inheritance and Interfaces

Lara Technologies Special-Six Test

CPS221 Lecture: Threads

Inheritance, and Polymorphism.

Rules and syntax for inheritance. The boring stuff

Questions Answer Key Questions Answer Key Questions Answer Key

Object Oriented Programming

Inheritance. Unit 8. Summary. 8.1 Inheritance. 8.2 Inheritance: example. Inheritance Overriding of methods and polymorphism The class Object

ECE Fall 2015, Second Exam

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

coe318 Lab 2 ComplexNumber objects

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

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Design Patterns: State, Bridge, Visitor

COMP Assignment #4 (Due: Mon. Mar 12 noon)

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Transcription:

Concepts of Object-Oriented Programming AS 2017 Exercise 6 Multiple Inheritance, Multiple Dispatch and Linearization November 3, 2017 Task 1 Consider the following C++ program: class X X(int p) : fx(p) int fx; ; class Y Y(int p) : fy(p) int fy; ; class B : public virtual X,public Y B(int p) : X(p-1),Y(p-2) ; class C : public virtual X,public Y C(int p) : X(p+1),Y(p+1) ; class D : public B, public C D(int p) : X(p-1), B(p-2), C(p+1) ; int main() D* d = new D(5); B* b = d; C* c = d; std::cout << b->fx << b->fy << c->fx << c->fy; return 0; What is the output of running the program? (a) 5555 (b) 2177 (c) 4147 (d) 7177 (e) 7777

(f) None of the above Task 2 Consider the following C++ code: class Person Person *spouse; string name; Person (string n) name = n; spouse = nullptr; bool marry (Person *p) if (p == this) return false; spouse = p; if (p) p->spouse = this; return true; ; Person *getspouse () return spouse; string getname () return name; The method marry is supposed to ensure that a person cannot marry him-/herself. Without changing the code above, create a new object that belongs to a subclass of Person and marry it with itself. Hint: use multiple inheritance. Explain exactly what happens. Task 3 Consider the following Java classes: class A public void foo (Object o) System.out.println("A"); class B public void foo (String o) System.out.println("B"); class C extends A public void foo (String s) System.out.println("C"); class D extends B public void foo (Object o) System.out.println("D"); class Main public static void main(string[] args) A a = new C(); a.foo("java"); C c = new C(); c.foo("java"); B b = new D(); b.foo("java"); D d = new D(); d.foo("java");

What is the output of the execution of the method main in class Main? (a) The code will print A C B D (b) The code will print A C B B (c) The code will print C C B B (d) The code will print C C B D (e) None of the above Task 4 Consider the following C# classes: public class Matrix public virtual Matrix add(matrix other) Console.WriteLine("Matrix/Matrix"); return null; public class SparseMatrix : Matrix public virtual SparseMatrix add(sparsematrix other) Console.WriteLine("SparseMatrix/SparseMatrix"); return null; public class MainClass public static void Main(string[] args) Matrix m = new Matrix(); Matrix s = new SparseMatrix(); add(m,m); add(m,s); add(s,m); add(s,s); public static Matrix add(matrix m1, Matrix m2) return m1.add(m2); A) What is the output of this program? Please explain. B) Without breaking modularity, change only the body of MainClass.add to make it possible to always call the most specific add method from the matrix hierarchy. Task 5 Java 8 allows interface methods to have a default implementation directly in the interface. A) What are some advantages of this feature? B) What could be some problems with this feature? How can they be resolved?

C) What problems of C++ multiple inheritance are avoided by this new design for Java interfaces? D) Now suppose that, in addition to method implementations, Java also allowed interfaces to define fields. Interfaces would not have constructors and interface fields would always be initialized with a default value. What are some advantages of this feature? Given the restrictions above, are there any problems left with such an implementation of multiple inheritance? If so what are they? Propose a solution for each problem you have identified. Task 6 Consider the following declarations in Scala: class C trait T extends C trait U extends C class D extends C Find all the types that can be created with or without traits, as well as the subtype relations between them. Task 7 Consider the following Scala code: class Cell private var x:int = 0 def get() = x def set(i:int) = x=i trait Doubling extends Cell override def set(i:int) = super.set(2*i) trait Incrementing extends Cell override def set(i:int) = super.set(i+1) A) What is the difference between the following objects? val a = new Cell val b = new Cell with Incrementing val c = new Cell with Incrementing with Doubling val d = new Cell with Doubling with Incrementing B) We use the following code to implement a cell that stores the argument of the set method multiplied by four: val e = new Cell with Doubling with Doubling Why doesn t it work? What does it do? How can we make it work?

C) Find a modularity problem in the above, or a similar, situation. Hint: a client that gets given a class C does not necessarily know if a trait T has been mixed in that class. D) We propose the following solution to support traits together with behavioral subtyping: Assume C is a class with specification S. Each time we create a new trait T that extends C, we must ensure that C with T also satisfies S. Show a counterexample that demonstrates that this approach does not work. Task 8 Consider the following Scala code: class A def bar() = "" trait B extends A override def bar() = super.bar()+"b" trait C extends B override def bar() = super.bar()+"c" trait D extends B override def bar() = super.bar()+"d" object Main def main() foo(new A with D with C with B()) def foo(x:a with D) println(x.bar()) What would be the output of the call Main.main()? (a) BDB (b) BBDBC (c) BBCBD (d) DB (e) BDC (f) BCD (g) None of the above Task 9 Consider the following C++ code (recall that default constructors, i.e., constructors without arguments, do not need to be called explicitly in C++): class A A(int i) std::cout << "A" << i; A() std::cout << "A1"; virtual int get()... ; class B: MODIFIER A B(int i) : A(i) std::cout << "B" << i;

; class C: MODIFIER A C(int i) : A(i) std::cout << "C" << i; ; class D: public B, public C D(int i) : B(i + 10), C(i + 20) std::cout << "D" << i; ; Now assume that MODIFIER is replaced by public. A) Why does the following client code not compile? void client() D* d = new D(5); std::cout << d->get(); B) Add a method to one of the classes so that client compiles. C) What is the output resulting from the call new D(5) in method client? D) Now, assume that MODIFIER is replaced by public virtual. What is the new output resulting from the call new D(5) in method client?