Variables vs Objects A variable Refers to an object May refer to different objects at different points in time An object can be referred to by several different variables at the same time Type of a variable Determines what classes of objects it may contain www.lloseng.com O Lethbridge/Laganiere 2001 Chapter 2: Review of object Orientation
© Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation 11 Variables vs. Objects A variable • Refers to an object • May refer to different objects at different points in time An object can be referred to by several different variables at the same time Type of a variable • Determines what classes of objects it may contain
Class variables a class variable's value is shared by all instances of a class Also called a static variable If one instance sets the value of a class variable then all the other instances see the same changed value Class variables are useful for -Default or constant' values(e.g PI) Lookup tables and similar structures Caution do not over-use class variables www.lloseng.com O Lethbridge/Laganiere 2001 Chapter 2: Review of object Orientation
© Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation 12 Class variables A class variable’s value is shared by all instances of a class. • Also called a static variable • If one instance sets the value of a class variable, then all the other instances see the same changed value. • Class variables are useful for: —Default or ‘constant’ values (e.g. PI) —Lookup tables and similar structures Caution: do not over-use class variables
2. 4 Methods, Operations and Polymorphism Operation a higher-level procedural abstraction that specifies a type of be ehaviour Independent of any code which implements that behaⅤiour -E. g, calculating area(in general) www.lloseng.com O Lethbridge/Laganiere 2001 Chapter 2: Review of object Orientation 13
© Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation 13 2.4 Methods, Operations and Polymorphism Operatio n • A higher-level procedural abstraction that specifies a type of behaviour • Independent of any code which implements that behaviour —E.g., calculating area (in general)
Methods, Operations and Polymorphism Method a procedural abstraction used to implement the behaviour of a class Several different classes can have methods with the same name -They implement the same abstract operation in ways suitable to each class e. g, calculating area in a rectangle is done differently from in a circle www.lloseng.com O Lethbridge/Laganiere 2001 Chapter 2: Review of object Orientation
© Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation 14 Methods, Operations and Polymorphism Method • A procedural abstraction used to implement the behaviour of a class. • Several different classes can have methods with the same name —They implement the same abstract operation in ways suitable to each class —E.g, calculating area in a rectangle is done differently from in a circle
Polymorphism a property of object oriented software by which an abstract operation may be performed in different ways in different classes Requires that there be multiple methods of the same name The choice of which one to execute depends on the object that is in a variable Reduces the need for programmers to code many if else or switch statements www.lloseng.com O Lethbridge/Laganiere 2001 Chapter 2: Review of object Orientation
© Lethbridge/Laganière 2001 Chapter 2: Review of Object Orientation 15 Polymorphism A property of object oriented software by which an abstract operation may be performed in different ways in different classes. • Requires that there be multiple methods of the same name • The choice of which one to execute depends on the object that is in a variable • Reduces the need for programmers to code many ifelse or switch statements