Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.

Slides:



Advertisements
Similar presentations
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
L3-1-S1 OO Concepts © M.E. Fayad SJSU -- CMPE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Inheritance. In this chapter, we will cover: The concept of inheritance Extending classes Overriding superclass methods Working with superclasses that.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Object-oriented Programming Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
CSE 501N Fall ‘09 14: Inheritance 20 October 2009 Nick Leidenfrost.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Object Oriented Software Development
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Coming up: Inheritance
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton January 12, 2016January 12, 2016January 12, 2016.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 19, 2005.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Design
Inheritance Allows extension and reuse of existing code
7. Inheritance and Polymorphism
Inheritance and Polymorphism
The Object-Oriented Thought Process Chapter 1
OOP What is problem? Solution? OOP
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences
Object Oriented Analysis and Design
Inheritance Basics Programming with Inheritance
Java Programming Language
Computer Programming with JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
ITEC 3220A Using and Designing Database Systems
Chapter 11 Class Inheritance
Presentation transcript:

Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015

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 10/18/20152

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

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 10/18/20154

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 10/18/20155

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 10/18/20156

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 10/18/20157

Member access modes in Java SpecifierClassPackageSubclassWorld PrivateYNNN No Specifier YYNN ProtectedYYYN PublicYYYY 10/18/20158

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/18/20159

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 –A class may implement multiple interfaces Its objects have more than one set of services Its objects have more than one set of services 10/18/201510

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 interface inheritance Class and interface inheritance 10/18/201511

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 10/18/201512

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 10/18/201513

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() 10/18/201514

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 10/18/201515

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 10/18/201516

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. 10/18/201517

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 10/18/201518

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 10/18/201519

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 10/18/201520

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 Polymorphism and inheritance are very powerful tool in software design Polymorphism and inheritance are very powerful tool in software design So powerful that if they are used properly, they can hurt really bad So powerful that if they are used properly, they can hurt really bad –If something goes wrong, it is hard to tell which class/object caused the problem 10/18/201521

Dynamic (late) binding Dynamic binding is a way of implementing polymorphism Dynamic binding is a way of implementing polymorphism It means that a function call is not linked to the actual function until execution time It means that a function call is not linked to the actual function until execution time Since a reference may be used to point to different types (subtypes or subclasses) of objects at execution time, so the actual function to be executed may not be known when the program is compiled. Since a reference may be used to point to different types (subtypes or subclasses) of objects at execution time, so the actual function to be executed may not be known when the program is compiled. The opposite of dynamic binding is static binding which links a function call to the actual function at compilation time. The opposite of dynamic binding is static binding which links a function call to the actual function at compilation time. 10/18/201522

Method Overriding A subclass overrides the operation(s) defined in the parent/grandparent classes. A subclass overrides the operation(s) defined in the parent/grandparent classes. This is for specialized subclasses to override the behavior defined in the super class(es). This is for specialized subclasses to override the behavior defined in the super class(es). For example: For example: –Superclass Employee’s print method prints employee’s SSN, Name, Address. –Its subclass Engineer’s print method prints engineer’s type and level in addition to employee’s normal information. 10/18/201523

Function overloading The same name is used for more than function The same name is used for more than function For example, a function of adding two integers may be named the same as the one of adding two floats: For example, a function of adding two integers may be named the same as the one of adding two floats: –Add(int x, int y); –Add(float x, float y); –Two functions use the same name for the same purpose with different data parameters 10/18/201524

Inheritance vs. Composition Two common techniques for reuse Two common techniques for reuse Class Inheritance Class Inheritance –White-box reuse –Defined at compile-time, statically –Cannot change parent at rum-time Object Composition Object Composition –Black-box reuse –A reference/pointer to the object –Can be changed at run-time Favor composition over inheritance Favor composition over inheritance 10/18/

Delegation A way to make composition as powerful as class inheritance A way to make composition as powerful as class inheritance A receiving object delegates requests to its delegates A receiving object delegates requests to its delegates == subclass defers request to parent class In class inheritance In class inheritance –parent can use this to access the child In delegation In delegation –Receiving object can pass itself to its delegate 10/18/

Three techniques for reuse Class Inheritance Class Inheritance –can provide default operations and let subclass override them –Cannot be changed at run-time Object Composition Object Composition –Can change the behavior being composed at run-time –Requires redirection and so becomes less efficient Generics Generics –Changes the types that a class can use –Cannot change at run-time 10/18/