Chapter 13. Object Oriented Programming

Similar documents
Inheritance, and Polymorphism.

Object-Oriented Technology. Rick Mercer

Simula. Where Would We Be Without It? By Joe Casey

Java Object Oriented Design. CSC207 Fall 2014

Objects and Classes. Lecture 10 of TDA 540 (Objektorienterad Programmering) Chalmers University of Technology Gothenburg University Fall 2017

ITI Introduction to Computing II

ITI Introduction to Computing II

Lecture 16: Object Programming Languages

Friend Functions, Inheritance

Smalltalk: developed at Xerox Palo Alto Research Center by the Learning Research Group in the 1970 s (Smalltalk-72, Smalltalk-76, Smalltalk-80)

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Topic 7: Algebraic Data Types

Chapter 9 :: Data Abstraction and Object Orientation

Inheritance and Polymorphism

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Programming for Mobile Computing

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Designing and Implementing Classes Topic 6

OBJECT ORİENTATİON ENCAPSULATİON

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Advanced Database Applications. Object Oriented Database Management Chapter 13 10/29/2016. Object DBMSs

Object-Oriented Programming and Design

Class 22: Inheritance

The Rise of OOP: Part 1 [The Early Years] Dina Lamdany

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Chapter 14 Abstract Classes and Interfaces

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

Subtyping (Dynamic Polymorphism)

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Chapter 10 :: Data Abstraction and Object Orientation

Object-Oriented Programming Concepts

Objects. object-oriented. programming.

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

What are the characteristics of Object Oriented programming language?

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Topic 5. Object Oriented Programming. Classes Are...

Topic 5. hierarchies.) The Business Of Software. CS 307 Fundamentals of Computer Science. Implementing Classes 1

CS-202 Introduction to Object Oriented Programming

Object Oriented Discrete-Event Simulation with OOSimL CS 4632

CSE 8B Programming Assignments Spring Programming: You will have 5 files all should be located in a dir. named PA3:

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

MechEng SE3 Lecture 7 Domain Modelling

CS1150 Principles of Computer Science Objects and Classes

Module 10 Inheritance, Virtual Functions, and Polymorphism

PROGRAMMING LANGUAGE 2

STUDY ON INHERITANCE OF CLASSES

Chapter 6 Introduction to Defining Classes

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Lecturer: William W.Y. Hsu. Programming Languages

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance, Polymorphism, and Interfaces

Inheritance, polymorphism, interfaces

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Basics of Object Oriented Programming FCITR MAGAZINE DR. SHAKEEL AHMAD

Programming in C# Inheritance and Polymorphism

Advanced Placement Computer Science. Inheritance and Polymorphism

Object Oriented Programming

C++ Programming: Introduction to C++ and OOP (Object Oriented Programming)

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

8. Polymorphism and Inheritance

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

1. Write two major differences between Object-oriented programming and procedural programming?

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Data Abstraction. Hwansoo Han

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

CS 251 Intermediate Programming Inheritance

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

index.pdf January 21,

2. The object-oriented paradigm

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Lecture 5: Inheritance

Inheritance. Transitivity

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor


The mechanism that allows us to extend the definition of a class without making any physical changes to the existing class is called inheritance.

Data Structures (INE2011)

CHAPTER 5 GENERAL OOP CONCEPTS

Inheritance and Substitution (Budd chapter 8, 10)

C++ Yanyan SHEN. slide 1

ITI Introduction to Computing II

Methods Common to all Classes

ITI Introduction to Computing II


ITI Introduction to Computing II

History C++ Design Goals. How successful? Significant constraints. Overview of C++

Everything is an object. Almost, but all objects are of type Object!

Chapter 15: Object Oriented Programming

PROGRAMMING LANGUAGE PARADIGMS & THE MAIN PRINCIPLES OF OBJECT-ORIENTED PROGRAMMING

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

CMSC 132: Object-Oriented Programming II

Inheritance and Polymorphism

Chapter 10 Object-Oriented Programming

CS1004: Intro to CS in Java, Spring 2005

OO is considered to be supported in three points which are usually symbolized as the three vertexes of a triangle: Encapsulació i ocultació

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

Computer Science 210: Data Structures

Transcription:

Chapter 13. Object Oriented Programming Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National University http://bi.snu.ac.kr

Computer Programming The history of computer programming is a steady move away from machine-oriented views of programming towards concepts and metaphors that more closely reflect the way in which we ourselves understand the world

Computer Programming Programming has progressed through: machine code assembly language machine-independent programming languages procedures & functions objects

History of Object-Oriented Programming Started out for simulation of complex man-machine systems, but was soon realized that it was suitable for all complex programming projects SIMULA I (1962-65) and Simula 67 (1967) were the first two objectoriented languages Developed at the Norwegian Computing Center, Oslo, Norway by Ole-Johan Dahl and Kristen Nygaard Simula 67 introduced most of the key concepts of object-oriented programming: objects and classes, subclasses ( inheritance ), virtual procedures

The Ideas Spread Alan Kay, Adele Goldberg and colleagues at Xerox PARC extend the ideas of Simula in developing Smalltalk (1970 s) Kay coins the term object oriented Smalltalk is first fully object oriented language Grasps that this is a new programming paradigm Integration of graphical user interfaces and interactive program execution Bjarne Stroustrup develops C++ (1980 s) Brings object oriented concepts into the C programming language

Objects class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; } } public void addminutes (int m) { int totalminutes = ((60*hour) + minute + m) % (24*60); if (totalminutes<0) totalminutes = totalminutes + (24*60); hour = totalminutes / 60; minute = totalminutes % 60; }

Objects class Time hour minute void addminutes( int m ) intime Attributes: hour = 8 minute = 30 Methods: void addminutes(int m) outtime Attributes: hour = 17 minute = 35 Methods: void addminutes(int m) objects

Classes and Objects A class is a prototype for creating objects When we write a program in an object-oriented language like Java, we define classes, which in turn are used to create objects A class has a constructor for creating objects

A Simple Class, called Time (partial) class Time { private int hour, minute; constructor for Time public Time (int h, int m) { hour = h; minute = m; } } public void addminutes (int m) { int totalminutes = ((60*hour) + minute + m) % (24*60); if (totalminutes<0) totalminutes = totalminutes + (24*60); hour = totalminutes / 60; minute = totalminutes % 60; }

Definition of an Object An object is a computational entity that: Encapsulates some state Is able to perform actions, or methods, on this state Communicates with other objects via message passing

Set of variables Encapsulates some state that describe an object s state These variables are sometimes called an object s attributes (or fields, or instance variables, or data members, or ) class Time { private int hour, minute; //...

Is able to perform actions, or methods, on this state An object can also include a group of procedures/functions that carry out actions class Time { private int hour, minute; public Time (int h, int m) { hour = h; minute = m; } a method of Time } public void addminutes (int m) { int totalminutes = ((60*hour) + minute + m) % (24*60); if (totalminutes<0) totalminutes = totalminutes + (24*60); hour = totalminutes / 60; minute = totalminutes % 60; }

Communicates with other objects via message passing Sends messages to objects, triggering methods in those objects In one of bill s methods, the following code appears: Time intowork = new Time(8, 30); intowork.addminutes(15); intowork.addminutes(15) bill Attributes: Methods: intowork Attributes: hour = 8 minute = 30 Methods: void addminutes(int m)

Structure of a Class Definition class name { declarations attributes and symbolic constants constructor definition(s) how to create and initialize objects } method definitions how to manipulate the state of objects

Advantages Building the system as a group of interacting objects: Allows extreme modularity between pieces of the system May better match the way we (humans) think about the problem Avoids recoding, increases code-reuse

Inheritance Classes can be arranged in a hierarchy Subclasses inherit attributes and methods from their parent classes This allows us to organize classes, and to avoid rewriting code new classes extend old classes, with little extra work! Allows for large, structured definitions

Example of Class Inheritance Object Objects made from this class, for example, have all the attributes and methods of the classes above them, all the way up the tree Shape color borderwidth tostring( ) equals( Object obj ) getclass( ) extends Color getcolor( ) void setborderwidth( int m ) extends Time hour minute void addminutes( int m ) Rectangle length width int computearea( ) extends Circle radius extends int computearea( ) extends Triangle base height int computearea( )

Polymorphism An object has multiple identities, based on its class inheritance tree It can be used in different ways A Circle is-a Shape is-a Object Shape color borderwidth Circle radius Object tostring( ) equals( Object obj ) getclass( ) extends Color getcolor( ) void setborderwidth( int m ) extends int computearea( )

How Objects are Created Circle c = new Circle( ); 1. 2. 3. Object Object Object Shape Shape Shape c Circle c Circle c Circle Execution Time

Using Polymorphism in Arrays We can declare an array to be filled with Shape objects, then put in Rectangles, Circles, or Triangles samples (an array of Shape objects) firstshape Attributes: length [2] = 17 width = 35 Methods: int computearea( ) secondshape Attributes: radius = 11 Methods: int computearea( ) thirdshape Attributes: base = 15 height = 7 Methods: int computearea( ) [0] [1] [2]

Using Polymorphism for Method Arguments Polymorphism give us a powerful way of writing code that can handle multiple types of objects, in a unified way public int calculatepaint (Shape myfigure) { final int PRICE = 5; } int totalcost = PRICE * myfigure.computearea( ); return totalcost;

Object-Oriented Programming in Industry Large projects are routinely programmed using objectoriented languages nowadays MS-Windows and applications in MS-Office all developed using object-oriented languages This is the world into which our students are graduating