Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.

Slides:



Advertisements
Similar presentations
Chapter 13 Abstraction and inheritance. This chapter discusses n Implementing abstraction. u extension u inheritance n Polymorphism/dynamic binding. n.
Advertisements

Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering ATOT - Lecture 23, 23 June Advanced Topics in Object Technology Bertrand Meyer.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Object-Oriented Software Testing. C-S 5462 Object-Oriented Software Testing Research confirms that testing methods proposed for procedural approach are.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
Inheritance in the Java programming language J. W. Rider.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Object Oriented Programming
1 Advanced Material The following slides contain advanced material and are optional.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Conformance Object-Oriented Programming Spring
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
CS 3180 (Prasad)L67Classes1 Classes and Interfaces Inheritance Polymorphism.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
Modern Programming Tools And Techniques-I
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Object-oriented Programming in Java
Inheritance Techniques
Programming Language Concepts (CIS 635)
Overloading and Constructors
Inheritance.
Class Invariants Pre-conditions and post-conditions describe the properties of individual methods. A class invariant is a global property of the instances.
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types

ceg860 (Prasad)L17IT2 Inheritance is subcontracting. class C {... void p(S s) { …; s.r(); … } Call: C c; T t; c.p(t); class S { … void r() { … }; } class T extends S { … void r() { … }; } Spec: pre-post conditions C S T

ceg860 (Prasad)L17IT3 Parent Invariant Rule The invariants of all the parents (and therefore, the ancestors) of a class apply to the class itself. Assertion Redeclaration Rule A routine redeclaration may only replace the original precondition by one equal or weaker, and the original postcondition by one equal or stronger. { P } S.r() { Q } { P*} T.r() { Q*} P => P* and Q* => Q (Cf. Rule of consequence) The postcondition associated with a 0-ary routine redeclared as an attribute manifests itself as an additional class invariant.

ceg860 (Prasad)L17IT4 Odds and Ends Global Inheritance Structure Java has a tree-structured class hierarchy (with null -type treated separately). The features of universal interest (such as equal(_), clone(), toString(), “locks for concurrency”, etc) are in class java.lang.Object. Frozen Features In Java, final can be applied to a class, a method, a field, etc to prohibit extension, redefinition, change, etc respectively. (This may be for security reasons, for freezing the semantics, for defining a constant, respectively.)

ceg860 (Prasad)L17IT5 Anchored Declarations : Covariance classfeature class Device feature alternate: Device; set_alternate(a:Device) is do alternate := a end end class class Printer inherit feature Device feature alternate: Printer; set_alternate(a:Printer) is do alternate := a end end Usually, redeclaration uses same signature to introduce or substitute a new algorithm. Eiffel also supports redeclaration by type specialization. classfeature class Device feature like Current alternate: like Current; set_alternate like Current (a:like Current) is do alternate := a end end

ceg860 (Prasad)L17IT6 Type Redeclaration Rule A redeclaration of a feature may replace the type of the feature, or the type of a formal, by any type that conforms to the original. This is a syntactic and purely static rule, with no effect on run-time objects. classfeature class Link[G] feature item : G; right : Link[G]; put_right(n:Link[G]) is do … end;......end; classfeature class BiLink[G] feature item : G; like Current left, right : like Current; like put_right(n: like left) is do … end; like put_left(n: like left) is do … end;......end; Link BiLink

ceg860 (Prasad)L17IT7 class Point { int x,y; boolean eq(Point p) { return ( x == p.x) && ( y == p.y) ; }} class ColoredPoint extends Point { Color c; boolean eq(Point p) { return ( x == p.x) && ( y == p.y) &&... ; }} // unsatisfactory class ColoredPoint extends Point { Color c; boolean eq(ColoredPoint p) { return ( super(p) ) && ( c == p.c ) ; }} // unsatisfactory override class ColoredPoint extends Point { Color c; boolean eq(Point p) { if (p instanceOf ColoredPoint) return ( super(p) ) && ( c == p.c ) ; else return false; }} // redeclare

ceg860 (Prasad)L17IT8 Static Typing Type violation: x.f(args) There is no f applicable to object associated with x, or args is not “acceptable”. statically typedA language is statically typed if it is equipped with consistency rules, enforceable by a compiler, whose observance by the software text guarantees that no execution of the system can cause a type violation. ( E.g., Eiffel, Ada, ML, etc.) strongly typedA language is strongly typed if it can guarantee the absence of type violations. ( E.g., Java, Smalltalk, etc.)

ceg860 (Prasad)L17IT9 “Nature of the beast”: Trying to guarantee that no computation will ever fail forces one to disallow computations that might succeed. ( E.g, n : integer = 2.0; is illegal in Ada.) Benefits : Reliability, Readability, Efficiency Typing vs Binding *Typing: When do we know for sure that at run-time there will be an operation corresponding to f and applicable to the object attached to x (with the argument arg ). »Polymorphism *Binding: Which operation will the call execute? »Redeclaration

ceg860 (Prasad)L17IT10 Typing problems Interaction with polymorphism (Covariance) Device d = new CD-Drive(); Printer p = new Printer(); d.set_alternate(p); Anchored declaration does not prevent type violation, but Java encoding seems to work. Interaction between redeclaration and descendant hiding Java prohibits method redeclaration that reduce visibility (e.g., from public to private ). O/w, it can always be “beaten” by promoting subclass object and using dynamic binding.