Stéphane Ducasse 1 Abstract Classes.

Slides:



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

S.Ducasse Stéphane Ducasse 1 Numbers.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
Stéphane Ducasse«ChapterNr».1 Abstract Classes Should not be instantiated (abstract in Java) But can defined complete methods Defines a protocol common.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Stéphane Ducasse 1 The Taste of Smalltalk.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Stéphane Ducasse6.1 Essential Concepts Why OO? What is OO? What are the benefits? What are the KEY concepts? Basis for all the lectures.
Some Basic Points on Classes
S.Ducasse Stéphane Ducasse 1 A Taste of Smalltalk.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Stéphane Ducasse 1 Objects to the Roots: Learning from beauty.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
Stéphane Ducasse«ChapterNr».1 Basic Objects, Conditionals and Loops Booleans Basic Loops Overview of the Collection hierarchy— more than 80 classes: (Bag,
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
The Java Programming Language
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
Stéphane Ducasse 1 Visitor.
Smalltalk (and Squeak) Aida Dungan and Rick Shreve.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
Stéphane Ducasse 1 Singleton.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Nathanael Schärli (SCG, University of Bern) and Andrew P. Black (CSE, Oregon Health & Science University) Nathanael Schärli (SCG, University of Bern) and.
Stéphane Ducasse7.1 Let’s Play Objects Simulate a LAN physically Set up a context for –future chapters –Exercises Some forward references to intriguate.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Stéphane Ducasse 1 A Little Journey in the Smalltalk Syntax.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Stéphane Ducasse 1 Elements of Design - Unit of Reuse.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level.
Sections Inheritance and Abstract Classes
CS0007: Introduction to Computer Programming
Let’s Play Objects.
CSC 205 Java Programming II
Types of Programming Languages
Programming Language Concepts (CIS 635)
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
CMSC 202 Java Primer 2.
Chapter 11: Classes, Instances, and Message-Handlers
Java Statements B.Ramamurthy CS114A, CS504 4/23/2019 BR.
CSE 3302 Programming Languages
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Presentation transcript:

Stéphane Ducasse 1 Abstract Classes

S.Ducasse 2 Goal Abstract classes Examples

S.Ducasse 3 Abstract Classes Should not be instantiated (abstract in Java) But can define complete methods. Defines a protocol common to a hierarchy of classes that is independent from the representation choices. A class is considered as abstract as soon as one of the methods to which it should respond to is not implemented (can be a inherited one).

S.Ducasse 4 Abstract Classes in Smalltalk Depending of the situation, override new to produce an error. No construct: Abstract methods send the message self subclassResponsibility Tools check this situation and exploit it. Abstract classes are not syntactically different from instantiable classes, but a common convention is to use class comments: So look at the class comment and write in the comment which methods are abstract and should be specialized.

S.Ducasse 5 Example Boolean>>not "Negation. Answer true if the receiver is false, answer false if the receiver is true." self subclassResponsibility

S.Ducasse 6 Goal Abstract classes Examples

S.Ducasse 7 Boolean Objects false and true are objects described by classes Boolean, True and False

S.Ducasse 8 Conditional: messages to booleans aBoolean ifTrue: aTrueBlock ifFalse: aFalseBlock aBoolean ifFalse: aFalseBlock ifTrue: aTrueBlock aBoolean ifTrue: aTrueBlock aBoolean ifFalse: aFalseBlock (thePacket isAddressedTo: self) ifTrue: [self print: thePacket] ifFalse: [super accept: thePacket] Hint: Take care — true is the boolean value and True is the class of true, its unique instance!

S.Ducasse Boolean Hierarchy How to implement in OO true and false without conditional? Late binding: Let the receiver decide! Same message on false and true produces different results

S.Ducasse 10 Example “Class Boolean is an abstract class that implements behavior common to true and false. Its subclasses are True and False. Subclasses must implement methods for logical operations &, not, controlling and:, or:, ifTrue:, ifFalse:, ifTrue:ifFalse:, ifFalse:ifTrue:” Boolean>>not "Negation. Answer true if the receiver is false, answer false if the receiver is true." self subclassResponsibility

S.Ducasse 11 Not false not -> true true not -> false Boolean>>not "Negation. Answer true if the receiver is false, answer false if the receiver is true.” self subclassResponsibility False>>not "Negation -- answer true since the receiver is false." ^true True>>not "Negation--answer false since the receiver is true." ^false

S.Ducasse 12 | (Or) true | true -> true true | false -> true true | anything -> true false | true -> true false | false -> false false | anything -> anything

S.Ducasse 13 Boolean>> | aBoolean "Evaluating disjunction (OR). Evaluate the argument. Answer true if either the receiver or the argument is true." self subclassResponsibility

S.Ducasse 14 False>> | aBoolean false | true -> true false | false -> false false | anything -> anything False>> | aBoolean "Evaluating disjunction (OR) -- answer with the argument, aBoolean." ^ aBoolean

S.Ducasse 15 True>> | aBoolean true | true -> true true | false -> true true | anything -> true True>> | aBoolean "Evaluating disjunction (OR) -- answer true since the receiver is true." ^ self

S.Ducasse 16 Boolean, True and False

S.Ducasse 17 Abstract/Concrete Abstract method Boolean>>not "Negation. Answer true if the receiver is false, answer false if the receiver is true." self subclassResponsibility Concrete method defined in terms of an abstract method Boolean>>xor: aBoolean "Exclusive OR. Answer true if the receiver is not equivalent to aBoolean." ^(self == aBoolean) not When not is be defined in subclasses, xor: is automatically defined

S.Ducasse 18 Block Use in Conditional? Why do conditional expressions use blocks? Because, when a message is sent, the receiver and the arguments of the message are always evaluated. Blocks are necessary to avoid evaluating both branches.

S.Ducasse 19 Implementation Note Note that the Virtual Machine shortcuts calls to boolean such as condition for speed reason. Virtual machines such as VisualWorks introduced a kind of macro expansion, an optimisation for essential methods and Just In Time (JIT) compilation. A method is executed once and afterwards it is compiled into native code. So the second time it is invoked, the native code will be executed.

S.Ducasse 20 Magnitude I'm abstract class that represents the objects that can be compared between each other such as numbers, dates, numbers. My subclasses should implement < aMagnitude = aMagnitude hash Here are some example of my protocol: 3 > 4 5 = max: 9 7 between: 5 and: 10

S.Ducasse 21 Magnitude Magnitude>> < aMagnitude ^self subclassResponsibility Magnitude>> = aMagnitude ^self subclassResponsibility Magnitude>> hash ^self subclassResponsibility

S.Ducasse 22 Magnitude Magnitude>> <= aMagnitude ^(self > aMagnitude) not Magnitude>> > aMagnitude ^aMagnitude < self Magnitude>> >= aMagnitude ^(self < aMagnitude) not Magnitude>> between: min and: max ^self >= min and: [self <= max]

S.Ducasse 23 Date Subclass of Magnitude Date today < Date newDay: 15 month: 10 year: > false

S.Ducasse 24 Date Date>>< aDate "Answer whether the argument, aDate, precedes the date of the rec." year = aDate year ifTrue: [^day < aDate day] ifFalse: [^year < aDate year]

S.Ducasse 25 Date Date>>= aDate "Answer whether the argument, aDate, is the same day as the receiver. " self species = aDate species ifTrue: [^day = aDate day & (year = aDate year)] ifFalse: [^false] Date>>hash ^(year hash bitShift: 3) bitXor: day

S.Ducasse 26 What you should know What is an abstract class? What can we do with it?