570 likes | 711 Views
Object-Oriented Concepts. A Review. Terminology. Object : A thing that exists in the domain of the problem E.g. An office building might have a number of ‘Elevators’, ‘Offices’, ‘SecurityDoors’, etc.
E N D
Object-Oriented Concepts A Review
Terminology • Object: A thing that exists in the domain of the problem • E.g. An office building might have a number of ‘Elevators’, ‘Offices’, ‘SecurityDoors’, etc. • Software is object-oriented if the design and implementation of that software is based on the interaction between the objects of the domain
Terminology • Class: A template used for the creation of objects • A class describes various attributes an object might have • e.g. A ‘Person’ class might have various attributes, including age, weight, height, eye colour, address, etc.
A Case Study A Grocery Store
A Case Study: Grocery Store • A grocery store has a number of ‘classes’ and ‘objects’ in its domain: • Shopping carts, shelves, cashiers, and various items • A shopping cart is a class or template that describes all shopping carts • Several instances of shopping carts, each with its own distinct properties, may exist in the grocery store • These shopping cart instances are objects in the grocery store
Grocery Store Classes Carrier Item Bread Fruit Basket Cart Apple Orange
Classes and Objects • Students often have trouble with the difference between classes and objects • ‘Apple’ in our example is a class • A grocery store may have hundreds of instances of this class • Large apples, small apples, red apples, green apples, etc.
Classes and Objects • Since ‘Apple’ is a class, and a class is a template, it makes sense that you can’t eat ‘Apple’ • However, you could eat ‘an apple’, which would be an instance of an Apple • It should be obvious that, if you eat an apple, the template ‘Apple’ still exists, as well as other instances • If you eat an apple, other apples to not disappear, nor does the existence of a fruit called an Apple
Classes and Objects • Thus, typically one interacts directly with class instances (objects) and not with classes themselves • In fact, in the object-oriented paradigm, any behaviour is defined using object interactions • For object-oriented software, an application is a collection of objects which interact to create software functionality
Classes and Objects • Let’s examine a software example • A Menu in a graphical user interface is an example of a class • An application may have many menus (File, Edit, Help, …) • This the application may have several Menu instances (objects)
Classes and Objects • The ‘Menu’ class may have several attributes: • A list of menu items • A background colour • A label (‘File’, ‘Edit’, ‘Help’) • ‘Menu’ may also have several actions that are possible: • show(): - make the menu visible • hide(): - make the menu invisible • addMenuItem(): - add a menu item to the list • setLabel(): - define the menu’s label
Why Classes? • Classes evolved through the concept of encapsulation • Encapsulation means taking all related elements and packaging them inside a single unit • In everyday terms, encapsulation means that objects should be specified in such a way, that they define their own behaviour and attributes
Encapsulation: Subroutines • Encapsulation was first applied to program behaviour alone • Programmers noticed that they had to repeat code that was similar or identical many times in a single program • e.g. The code to print a line of text to the monitor, the code to multiple two numbers, etc.
Encapsulation: Subroutines • Eventually, programmers began placing this code into accessible areas of memory • This code would be called repeatedly • These reusable code units were called subroutines • Although modern programmers call them functions, procedures, or methods
Encapsulation: Objects • The first incarnations of objects were called ‘records’ • They typically placed related information together into a single data structure • In C/C++: structs • In Pascal: records • e.g. For a person, one might group age, height, weight, address, and other information into a single record: ‘person’
Encapsulation: Objects • Code that manipulated that data was kept separate • e.g. void movePerson(person p1, char *newAddr) { p1.address = newAddr; }
Encapsulation: Objects • Eventually, they began placing functionality (as well as data) into a single unit • Thus, the class was born • One side effect of object encapsulation is that the data becomes somewhat irrelevant • Since the behaviour is what is important, the functionality can be achieved using data in any way possible • Thus, what data is stored, and how it is stored is irrelevant outside the object itself • Objects, in general, contain persistent data • This data ‘persists’ from one call to another
Information Hiding • Usually, since the way data is stored and used in an object is irrelevant outside the object, it is kept hidden • In object-oriented systems, this data is called private data or hidden data • E.g. In Java and C++, hidden data is achieved through private class variables
Implementation Hiding • Also the method in which data is manipulated to accomplish behaviour is irrelevant outside the object • The behaviour provided by an object is defined by the object’s interface • In fact, two different objects that provide the same behaviour could be used interchangeably as long as they share the same interface
Interfaces • An interface is simply a description of the functionality defined by an object • An interface typically contains a list of available operations, as well as a description of the data passed in and out of these operations
An Example CashRegister interface total : num enterItem() Implementation of enterItem() getTotal() Implementation of getTotal() pay() Implementation of pay() • This cash register simply keeps a running total • Likely the receipt includes only the total due
An Example DetailedCashRegister interface items : List<Item> enterItem() Implementation of enterItem() getTotal() Implementation of getTotal() pay() Implementation of pay() • This cash register keeps track of all items • Likely the receipt displays all items and their prices
An Example • Since the objects have identical interfaces, they could be used interchangeably • However, the two cash registers do their job differently • Both objects in this example were only accessible through their ‘interface’ • Which normally is a set of externally accessible operations and not data items themselves
An Visual Idea Interface Implementation Data
Classes • Let’s expand our definition of classes, to specifically refer to classes in software • Classes define everything that is common to all instances: • Definitions of member operations • The actual code is defined with the class • Declarations of data members • The data type and name of the data members is defined with the class
Objects • Objects, on the other hand, contain everything that is different with each instance • The values of the data members is an example • Even though the data members are defined in the class definition, the actual memory allocated to store that data is associated with each object, not the class
Objects • In addition, the member operations on a class must be associated with a specific object, so they can access member data for that specific object • There are two ways this can be done: • The member operations themselves can be defined on each object • Thus, operations are duplicated for each instance • The operations, when called, must be told which object is involved • For example, a reference or handle to the object could be passed along with the input parameters
Exceptions to this Rule • Sometimes, items normally associated with an object instance, are associated with the class itself • Operations that do not access member variables or member operations • Variables that should contain the same value for all instances • When the value is changed using one instance, the value on the other instances should also change • In Java and C++, such members are called ‘static’ variables and ‘static’ operations • In other languages, they are called class variables and class operations
Messages • Messages are how objects interact • Messages typically involve an object making an invocation of another object’s operation • Messages contain the following information: • A handle (or reference) of the object whose operation is to be called • The name of the operation • A number of arguments to be passed to/from the operation • This could include input parameters, output parameters, or parameters that are used for input and output
Inheritance • In the previous lecture, we saw a diagram illustrating relationships between classes • One relationship classes can have is inheritance • A class A inheritsfrom another class, B, if it represents something more specific, but A is still a type of B • e.g. Apple is a type of Fruit • e.g. Notebook is a type of Computer • e.g. JPEGImage is a type of Image
Inheritance • If class A inherits from class B • A is a subclass of B • B is a superclass of A • Questions: • Is it possible for a class to have more than one subclass? • Is it possible for a class to have more than one superclass?
Multiple Inheritance • Multiple inheritance makes sense in the real world: • Here is an example: • Susan works at a bank, thus she is an instance of the type ‘Banker’ • Susan plays professional soccer, thus she is an instance of the type ‘SoccerPlayer’ • Possibly, SoccerPlayer is a subclass of a class called Athlete or something similar • Therefore, Susan must be an instance of some class, that is a subclass (or descendant) of SoccerPlayer and a subclass (or descendant) of Banker
Multiple Inheritance • Java does not support pure multiple inheritance • A Java class can inherit from only (and exactly) one class • If none is specified, Object is assumed • A Java class can, however, implement as many interfaces as you want
Multiple Inheritance • C++ classes can inherit from multiple classes • Here is an example: class A : public B, public C, public D { … }
Polymorphism • While polymorphism seems simple, it creates complications for running programs • For example, look at this statement in Java: Printer printer = …; printer.print(Document doc); • Which version (on which subclass) of the ‘print’ method will be called? • It depends on the type of object stored in ‘printer’
Polymorphism • Here’s another example: • There is a class called ‘Car’ which defines a method called ‘accelerate’ • One subclass of ‘Car’ called ‘AutomaticCar’ would define ‘accelerate’ in one way, while another subclass ‘ManualCar’ would define it another way
Dynamic vs. Static Binding • In the example, we learned that sometimes a runtime environment must determine which version of an operation to execute at run time • This is known as dynamic binding • Normally, the operation to be executed is determined at compile time • This is known as static binding
Overriding • Overriding is when a subclass defines an operation that is already defined on the superclass • Using dynamic binding, the subclass’ version of the operation will be executed when a variable contains an instance of the subclass
Overloading • Overloading is a similar concept to overriding • An operation (or operator, such as +, =, …) can be defined more than once to operate on different arguments • Thus a given operation name may be reused for several operations, as long as the signature is different • An operation’s signature or template is its name, as well as the number, order, and types of its arguments
Genericity • Genericity is the term applied to situations where code operates on data, whose type is unknown until run time • An example might be creating a quicksort operation which takes an array • If this operation supports genericity, the type of the objects in the array could be unknown until the program is actually run • Now, the same operation can be used to sort integers, strings, etc. by simply associating the operation with the appropriate type within the program
Summary • So far, I have introduced the following vital ingredients in object-oriented development: • Encapsulation • Classes • Objects • Information hiding • Implementation hiding and interfaces • Inheritance • Messages • Polymorphism • Overriding • Overloading • Genericity
Summary • To give you a foundation, I’ll now give you examples of each in some OOP languages • I will give Java examples • In some cases, Java5 will be used when a feature is not available in previous versions
Encapsulation • Java, C++, and Eiffel use ‘classes’ for encapsulation • In all 3 languages, operations and attributes can be combined into a single unit • In Java: public class Person { private int age = 0; void birthday() { age++; } }
Encapsulation • The spirit of encapsulation is grouping related concepts • Thus classes provide attributes to store data about an entity • Classes provide operations to manipulate the entity in some way
Information Hiding • The example also uses information hiding since, the attributes are declared as private • That means that the attribute cannot be modified directly, like this: Person person = …; person.age = 14; // this is illegal
Interfaces • Implementation hiding can be reinforced by using interfaces • In Java (stored in AgedEntity.java and Person.java): public interface AgedEntity { void birthday(); } public class Person implements AgedEntity { private int age = 0; void birthday() { age++; } }
Inheritance • Java supports single inheritance: public class A extends B { … }
Messages • Messages in Java occur in the form of method invocations • In Java: obj.doSomething(11); or person.birthday();
Polymorphism in Java • Dynamic binding is automatically used in Java, so polymorphism is a natural consequence: public abstract class A { private int a = 0; abstract void doSomething(int val); } public class B extends A { void doSomething(int val) { a = val; } } public class C extends A { void doSomething(int val) { a = val * val; } }
Polymorphism in Java • Consider the following lines of code: A obj = null; obj = new B(); obj.doSomething(15); // obj.a should be 15 (a = val) obj = new C(); obj.doSomething(15); // obj.a should be 225 (a = val * val)