Ranga Rodrigo. Class is central to object oriented programming.

Slides:



Advertisements
Similar presentations
Copyright W. Howden1 Programming by Contract CSE 111 6/4/2014.
Advertisements

Ceg860(Prasad)L10DC1 Design by Contract Invariants Pre-post conditions : Rights and Obligations Exceptions : Contract Violations.
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
P5, M1, D1.
Data Abstraction II SWE 619 Software Construction Last Modified, Spring 2009 Paul Ammann.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Software Engineering and Design Principles Chapter 1.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Software Testing and Quality Assurance
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
Information Hiding and Encapsulation
Terms and Rules Professor Evan Korth New York University (All rights reserved)
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Eiffel Language and Design by Contract Contract –An agreement between the client and the supplier Characteristics –Expects some benefits and is prepared.
Computer Science 340 Software Design & Testing Design By Contract.
Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams.
Chapter 10 Introduction to Components. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFramework Detailed.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
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.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Two Parts of Every ADT An abstract data type (ADT)  is a type for encapsulating related data  is abstract in the sense that it hides distracting implementation.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
SWE 316: Software Design and Architecture Objectives Lecture # 18 Introduction to Components SWE 316: Software Design and Architecture To learn:  benefits.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
Design by Contract Ranga Rodrigo based on Mark Priestley's Lectures.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Object Oriented Programming
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Debugging COMP T1.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
L13: Design by Contract Definition Reliability Correctness Pre- and post-condition Asserts and Exceptions Weak & Strong Conditions Class invariants Conditions.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
More About Classes Ranga Rodrigo. Quiz: Question 1 Write the following C++ code in Eiffel. void covert { int i ; float f(1.41) ; i = (int) f ; }
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.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Unified Modeling Language (UML)
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chapter 6 CS 3370 – C++ Functions.
Ranga Rodrigo Based on Marc Priestley's Lectures
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Indexer AKEEL AHMED.
Classes and Objects 2nd Lecture
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Slides by Steve Armstrong LeTourneau University Longview, TX
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Object-Oriented Programming
Design by contract Object-Oriented Software Construction by Bertrand Meyer, Prentice Hall The presence of a precondition or postcondition in a routine.
Outline Anatomy of a Class Encapsulation Anatomy of a Method
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Classes and Objects Systems Programming.
Presentation transcript:

Ranga Rodrigo

Class is central to object oriented programming.

Classes Classes are abstractions of real-world entities. Classes support data abstraction, by providing an operational interface to some protected data. They make software components easily reusable.

Classes Versus Objects Blueprint Class Buildings Objects

Many Objects of a Class Class Object 1…Object n

Attributes Attributes are synonymously known as Member variables In Eiffel Attributes are features.

Methods Methods are synonymously known as Member functions (functions and procedures) In Eiffel Methods are features.

Features AttributesMethods Functions (return things) Procedures (don not return things)

Counter Example Counters store (and make available through a display) a number. Every time the user clicks a button, or similar interface widget, the number stored is increased. There is a reset button, to set the count back to 0. There will usually be a maximum permissible count value.

Attributes in Our Counter The role of a counter is to store a numeric value. We can define an attribute which stores the current value of the counter.

class COUNTER feature value : INTEGER end COUNTER Class in Counter.e File

value Attribute This attribute is visible to all classes that use the counter class (client code). In Java or C++ it is normal to make attributes private to prevent client code changing the value of the attribute, and to protect client code from modification if the representation of the attribute changes. In Eiffel, these benefits are supported in different ways Client classes have read-only access to attributes. Uniform access.

Read-Only Access to Attributes

class MAIN create make feature c : COUNTER make is do create c io.put_integer(c.value) --Read access ok c.value := 42 --Compilation error here end

Uniform Access If the type of an attribute is changed, the original attribute can be replaced by a function. Eiffel's syntax ensures that client code will not be affected by such a change. This is known as the Uniform Access Principle. There is no way for client code to tell whether an “attribute” of a class is implemented as an attribute or as a function.

class COUNTER feature value_rep : REAL value : INTEGER is do Result := value_rep.floor end

Class Invariants The collection of attributes in a class is sometimes said to define the state of instances of the class. A class invariant is a property which specifies which are the legitimate values of a class's attributes.

class COUNTER feature value : INTEGER invariant value_in_range: 0 <= value and then value < 1000 end

Class Invariants An invariant section can be written at the end of a class text, and can contain any number of Boolean conditions. Each condition can be given a label to help identify it. There are two main benefits: Documentation. Debugging: an Eiffel project can be configured to check invariants at run-time.

Constructors Constructors, or creation routines as they are called in Eiffel, initialize attribute values when a new object is created.

class COUNTER creation make feature value : INTEGER maximum : INTEGER make( max : INTEGER ) is do maximum := max end invariant value_in_range: 0 <= value and value <= maximum end

Calling the Constructor Because make is defined here to be a creation routine, whenever an instance of the counter class is created, the desired constructor must be called: create c.make(9999)

Pre- and Post-Conditions We should check that the parameters, if any, are valid. The conditions that check these are called the routine's preconditions and are written in Eiffel in a require clause before the routine's body. Preconditions We should check that the routine has done what it should. The conditions that check this are called the routine's postconditions and are written in Eiffel in an ensure clause after the routine's body. Postconditions

make( max : INTEGER ) is require valid_maximum: max > 0 do maximum := max ensure value_initialized: value = 0 max_initialized: maximum = max end

increment is require not_at_maximum: value < maximum do value := value + 1 ensure value_incremented: value = old value + 1 maximum_constant: maximum = old maximum end Old Sometimes, a postcondition should compare the value before the routine ran with its value after. The original value can be accessed using the keyword old.

Precondition The precondition states that the routine can only be successfully called if the maximum value has not been reached. Notice that the body of the routine does not check that the value is less than the maximum. As we will see later, this is part of the design by contract (DBC) philosophy.

Precondition We should now add to the class a function which allows clients to check whether or not the counter is at the maximum value. Without this, they have no way of checking that the precondition is true before calling increment. A function is a routine that returns a value. Each function has a predefined local variable called Result : the value in this variable when control returns to the caller is the return value of the function. The return value can be specified by using Result in the function's postcondition.

at_max : BOOLEAN is do if value < maximum then Result := false else Result := true end ensure Result = (value = maximum) end

Incrementing It would be better to use this function to define the precondition of increment: this means that the details of the check are written in one place only, and hence that the code is more maintainable. increment is require not_at_maximum: not at_max do... end

Design by Contract (DBC) One important aspect of design by contract is the specification of classes using invariants, preconditions and post-conditions.

DBC InvariantsPreconditions Post- conditions

EiffelStudio Support for DBC EiffelStudio allows controls over which aspects of a class's specification are checked at runtime. Typically, you might check everything when developing a program, and only preconditions when it was delivered to a client. Contract view shows the specification but omits the implementation of routines. This gives enough information for a client to use the class.

DBC in Java Java does not support DBC in the integrated way that Eiffel does. Assertion facility which can be used to provide some of the same functionality. Program must be run with enableassertions or es flag. java -ea Main Then an exception will be raised and the execution terminated if any assertion is false

public class Counter { private int value ; private int maximum ; public Counter(int max) { assert max > 0 : "Precondition failure: Counter.Counter" ; value = 0 ; maximum = max ; call_invariant() ; assert value == 0 && maximum == max : "Postcondition failure: Counter.Counter" ; } CONTD.

public int getValue() { return value ; } public void call_invariant() { assert 0 <= value && value <= maximum : "Counter invariant failure" ; } CONTD.

Eiffel and Java DBC: Differences Eiffel syntax clearly separates the contract of the class from its implementation. Java provides no way of calling the class invariant automatically at the appropriate times. Java does not provide an easy way of accessing the old values of attributes.