Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences

Slides:



Advertisements
Similar presentations
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Advertisements

CS 211 Inheritance AAA.
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:
ITEC200 – Week03 Inheritance and Class Hierarchies.
L3-1-S1 OO Concepts © M.E. Fayad SJSU -- CMPE Software System Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room.
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
Object Oriented Software Development
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
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.
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.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
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
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
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
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton January 12, 2016January 12, 2016January 12, 2016.
Classes, Interfaces and Packages
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.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Introduction to Object-oriented Programming
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
Module Road Map Refactoring Why Refactoring? Examples
Inheritance Allows extension and reuse of existing code
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Nested class.
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
Exceptions An exception signals an error, and has the ability to propagate upward through the call stack for easier management To “raise” an exception,
Object Oriented Analysis and Design
Inheritance Basics Programming with Inheritance
Overview of Eclipse Lectures
Java Programming Language
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Advanced Java Programming
Computer Programming with JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Object-Oriented Programming: Inheritance and Polymorphism
Inheritance.
Inheritance and Polymorphism
Object Oriented Analysis and Design
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences 8/8/2018 Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton August 8, 2018

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

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

Classes and objects in software 8/8/2018 Classes and objects in software 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 Template of objects of the same type Write once and use many times Implementation classes/objects Some classes/objects do not match to concepts in the real world List, stack, tree, queue – containers Exception classes 8/8/2018

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

Attributes and Operations 8/8/2018 Attributes and Operations 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 Can be accessed in both instance and class operations Class operations (static operations) Used to access class attributes Cannot access instance attributes and operations 8/8/2018

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

Member access modes in Java 8/8/2018 Member access modes in Java Specifier Class Package Subclass World Private Y N No Specifier Protected Public 8/8/2018

Abstract and concrete classes 8/8/2018 Abstract and concrete 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 That are not abstract classes Can be instantiated Concrete subclasses implement all the operations 8/8/2018

Interfaces Signature of an operation: Interface: Operation name 8/8/2018 Interfaces Signature of an operation: Operation name Objects/classes it takes as parameters Return value and type 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 8/8/2018

Inheritance Specifies is-a or a-kind-of relationship 8/8/2018 Inheritance Specifies is-a or a-kind-of relationship Generalization and specialization Superclasses and subclasses Single and multiple inheritance Class and interface inheritance 8/8/2018

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

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

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

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

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

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

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

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

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

8/8/2018 Polymorphism 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 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 8/8/2018

Dynamic (late) binding 8/8/2018 Dynamic (late) binding Dynamic binding is a way of implementing polymorphism 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. The opposite of dynamic binding is static binding which links a function call to the actual function at compilation time. 8/8/2018

8/8/2018 Method Overriding 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). 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. 8/8/2018

Function overloading The same name is used for more than one function 8/8/2018 Function overloading The same name is used for more than one function 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 8/8/2018

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

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

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

8/8/2018 Nested Classes in Java A class, nested class, that is defined in another class, enclosing class. A nested class can be static and non-static Static nested called static nested classes Non-static class called inner classes A nested class is a member of the enclosing class. An inner class has an implicit reference to its enclosing class A static nested class does NOT have any reference to its enclosing class 8/8/2018

8/8/2018 Static Nested Classes A static nested class (SNC) is associated with its enclosing class. A SNC cannot refer to instance members of the enclosing class. A SNC interacts with the instance members of its enclosing class and other classes just like any other top-level class A SNC may be declared public, protected, private, and package private. A SNC is behaviorally a top-level class. 8/8/2018

8/8/2018 Inner Classes An inner class is associated with an instance of its enclosing class. It has direct access to that instance’s members, even private members. An inner class CANNOT define any static members An inner class CANNOT define static initializers An inner class CANNOT define interfaces An inner class may define static final compile-time constants Two special kinds of inner classes Local classes Anonymous classes 8/8/2018

Local Classes A local class is a class that is defined in a block. 8/8/2018 Local Classes A local class is a class that is defined in a block. A block is a group of statements between two matching parenthesis. A local class is typically defined in a method. A local class has access to the members of its enclosing class A local class can only access local variables in the block, that are declared final. Since SE 8, it can access local variables that are final or effectively final Effectively final – whose value is never changed after initialized. Since SE 8, it can access the parameters of the method in which (as the block) the class is declared. 8/8/2018

Anonymous Classes An anonymous class is a local class without a name 8/8/2018 Anonymous Classes An anonymous class is a local class without a name You can only use it once Anonymous classes are used only in expressions Declaration components The new operator The name of an interface or a class to extend Parentheses that contain the arguments to a constructor A constructor of the class to extend No arguments if it is an interface A declaration body of the anonymous class 8/8/2018

8/8/2018 Anonymous Classes An anonymous class (AC) can access the members of the enclosing class An AC can only access local variables what are final or effectively final An AC cannot declare static members unless they are constant variables An AC can declare: Instance fields Instance extra methods (not defined in the superclass) They only be called inside the AC Instance initializers Local classes 8/8/2018

Static and Instance Initializers 8/8/2018 Static and Instance Initializers A static initializer may be used to initialize static variables with complex logic, e.g., JDBC connection. public class ClassWithStaticInitializer { static { // statements of static initializer } //other members } A class may have any # of static initializers and they are executed in the order they appear in the class declaration A class may define instance initializers that are executed before each constructor is executed. public class ClassWithInstanceInitializer { { // statements of instance initializer } //other members including constructors Instance initializers will be called in the order they appear. Instance initializers may be used to define common parts for all constructors of the class. 8/8/2018

Toolkits A set of predefined classes Provides useful, general-purpose functionality Collection classes (list, stack, etc) GWT (Google Web Toolkit) Does NOT impose any design level decision Code Reuse 8/8/2018

Frameworks A reusable design for a type of applications Dictates the architecture of your app Overall structure Key components and their responsibilities How classes/objects interact with each other Design reuse Inversion of Control Dependency Injection 8/8/2018

Frameworks Framework examples: Web Apps J2EE, Spring Framework, .Net ORM (object to relation mapping) Hibernate, OpenJPA, MyBatis Mobile Apps Android iOS 8/8/2018