Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 19, 2005.

Similar presentations


Presentation on theme: "Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 19, 2005."— Presentation transcript:

1 Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 19, 2005

2 Classes and objects for modeling Classes Classes –Real world concepts –Student, course, bicycle, etc Objects Objects –Instances of real word concepts –John Smith, Operating Systems, Mongoose Mountain S100, etc

3 Classes and objects for modeling “Customers John and Susan entered the MakeMoney bank and were served by teller Andy.” Classes Objects:

4 Classes and objects in software Objects Objects –Packaging both data and the procedures that operate on the data into one –Operations are the only way to change the data items – encapsulation Classes Classes –Template of objects of the same type –Write once and use many times Implementation classes/objects Implementation classes/objects –Some classes/objects do not match to concepts in the real world List, stack, tree, queue – containers List, stack, tree, queue – containers Exception classes Exception classes

5 Attributes and operations Instance attributes (simply attributes) Instance attributes (simply attributes) –Properties (internal state) of an object Student: name, major, class, GPA Student: name, major, class, GPA Course: title, # of credits, description Course: title, # of credits, description –Each object has its own value for an attribute Instance operations (simply operations) Instance operations (simply operations) –Services of an object Stack: push, pop, isEmpty, isFull Stack: push, pop, isEmpty, isFull –Encapsulation Attributes (private and protected) are not accessible directly by client Attributes (private and protected) are not accessible directly by client Operations are used to read/write the values of attributes Operations are used to read/write the values of attributes

6 Attributes and Operations Class attributes (static attributes) Class attributes (static attributes) –Represent properties that can be applied to all objects of the same class –Only one value for each class attribute shared by all objects of the class All accounts of the CheckingAccount class have the same interest rate All accounts of the CheckingAccount class have the same interest rate –Can be accessed in both instance and class operations Class operations (static operations) Class operations (static operations) –Used to access class attributes –Cannot access instance attributes and operations

7 Class member access modes Public Public –Public attributes and public operations –Accessible to all clients or the world Protected Protected –Protected attributes and operations –Accessible to the class and its subclasses Private Private –Private attributes and operations –Only accessible by the operations of the class

8 Member access modes in Java SpecifierClassPackageSubclassWorld PrivateYNNN No Specifier YYNN ProtectedYYYN PublicYYYY

9 Abstract and concrete classes Abstract classes Abstract classes –Define a common interface for its subclasses –Defer some or all of its implementation of operations to its subclasses –Cannot be instantiated Concrete classes Concrete classes –That are not abstract classes –Can be instantiated –Concrete subclasses implement all the operations

10 Interfaces Signature of an operation: Signature of an operation: –Operation name –Objects/classes it takes as parameters –Return value and type Interface: Interface: –The set of signatures of related operations –Representing a capability or a set of services –An object may implement multiple interfaces

11 Inheritance Specifies is-a or a-kind-of relationship Specifies is-a or a-kind-of relationship Generalization and specialization Generalization and specialization Superclasses and subclasses Superclasses and subclasses Single and multiple inheritance Single and multiple inheritance Class and implementation inheritance Class and implementation inheritance

12 Superclasses and subclasses Superclass Superclass –Also called base class or parent class –All of its members are inherited by its subclasses Subclasses Subclasses –Also called child classes –Inherit all the properties of its superclasses

13 Single and multiple inheritance Single inheritance Single inheritance –A subclass cannot have more than one parent Multiple inheritance Multiple inheritance –A subclass may have more than one parent –Java does not allow multiple inheritance –Java allows multiple interface inheritance Through interface implementation Through interface implementation

14 Class inheritance lattice Person SSN Name getSSN() getName() Faculty office getOffice() Staff dept getDept() Student major getMajor() FullTime salary getSalary() PartTime PayRate getPayRate() Graduate PayRate getPayRate() Underg PayRate getPayRate() TA labs getLabs()

15 Implementation inheritance Also called class inheritance Also called class inheritance Define an object’s implementation in terms of another object’s implementation Define an object’s implementation in terms of another object’s implementation Pure class inheritance in C++ Pure class inheritance in C++ Pure class inheritance in Java Pure class inheritance in Java

16 Pure class inheritance with C++ Class BinaryTree { getRoot() {;} setRoot() {;} leftTree() {;} rightTree() {;} } Class BinSearchTree: private BinaryTree { insert() {;} remove() {;} find() {;} } They use the operations of BinaryTree The operations of BinaryTree are not accessible/visible to the clients of BinSearchTree because BinaryTree is private

17 Pure class inheritance with Java Java does not allow private parent Java does not allow private parent The parent and grandparents are public and accessible through the subclass The parent and grandparents are public and accessible through the subclass Java cannot implement pure implementation inheritance. Java cannot implement pure implementation inheritance.

18 Interface inheritance Describe when an object can be used in place of another Describe when an object can be used in place of another Pure interface inheritance in C++ Pure interface inheritance in C++ –Superclasses are pure abstract classes Pure interface inheritance in Java Pure interface inheritance in Java –Superclasses are Java interfaces

19 Interface inheritance with C++ Class Stack { virtual push() = 0; virtual pop() = 0; virtual isEmpty() = 0; virtual isFull() = 0; } Class MyStack: public Stack { push() {;} pop() {;} isEmpty() {;} isFull() {;} } Class YourStack: public Stack { push() {;} pop() {;} isEmpty() {;} isFull() {;} } A C++ Abstract class

20 Interface inheritance with Java Interface Stack { push();pop();isEmpty(); isFull() 0; } Class MyStack implements Stack { push() {;} pop() {;} isEmpty() {;} isFull() {;} } Class YourStack implements Stack { push() {;} pop() {;} isEmpty() {;} isFull() {;} } A Java Interface

21 Polymorphism When a client sends a request to a reference, the method executed depends on the object behind the reference. When a client sends a request to a reference, the method executed depends on the object behind the reference.

22 Dynamic (late) binding

23 Method overriding

24 Function overloading


Download ppt "Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 19, 2005."

Similar presentations


Ads by Google