Introduction to Object-Oriented Programming



Similar documents
Introduction to Object-Oriented Programming

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

The Interface Concept

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Contents. Java - An Introduction. Java Milestones. Java and its Evolution

Chapter 1 Java Program Design and Development

Lecture 7 Notes: Object-Oriented Programming (OOP) and Inheritance

Moving from CS 61A Scheme to CS 61B Java

Chapter 1 Fundamentals of Java Programming

History OOP languages Year Language 1967 Simula Smalltalk

LAB4 Making Classes and Objects

LAB 1. Familiarization of Rational Rose Environment And UML for small Java Application Development

Glossary of Object Oriented Terms

Chapter 13 - Inheritance

Java CPD (I) Frans Coenen Department of Computer Science

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Installing Java. Table of contents

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

11 November

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

Software Engineering Techniques

Introduction to Object-Oriented Programming

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

1001ICT Introduction To Programming Lecture Notes

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Object Oriented Design

C++ INTERVIEW QUESTIONS

CS170 Lab 11 Abstract Data Types & Objects

public static void main(string[] args) { System.out.println("hello, world"); } }

Advanced Data Structures

AP Computer Science Java Subset

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Install Java Development Kit (JDK) 1.8

Software Design Principles and Guidelines

Crash Course in Java

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

CSC 551: Web Programming. Spring 2004

An Overview of Java. overview-1

Java Crash Course Part I

Introduction to programming

1/20/2016 INTRODUCTION

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

CS506 Web Design and Development Solved Online Quiz No. 01

C# and Other Languages

AP Computer Science A - Syllabus Overview of AP Computer Science A Computer Facilities

Java Classes. GEEN163 Introduction to Computer Programming

CSC230 Getting Starting in C. Tyler Bletsch

CS193j, Stanford Handout #10 OOP 3

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Java Interview Questions and Answers

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CSE 1020 Introduction to Computer Science I A sample nal exam

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Introduction to Object-Oriented Programming in MATLAB

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

The C Programming Language course syllabus associate level

C++ Programming Language

Object-Oriented Programming in Java

Inheritance, overloading and overriding

Java (12 Weeks) Introduction to Java Programming Language

Object-Oriented Programming

Introduction: Abstract Data Types and Java Review

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

CSC 551: Web Programming. Fall 2001

Introduction to Programming Block Tutorial C/C++

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Some Fair Trade Recipes to get you started

Software Quality Factors OOA, OOD, and OOP Object-oriented techniques enhance key external and internal software quality factors, e.g., 1. External (v

CEC225 COURSE COMPACT

Managing Variability in Software Architectures 1 Felix Bachmann*

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Part I. Multiple Choice Questions (2 points each):

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Object Oriented Programming and the Objective-C Programming Language 1.0. (Retired Document)

I PUC - Computer Science. Practical s Syllabus. Contents

Programming Methods & Java Examples

To Java SE 8, and Beyond (Plan B)

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Building Java Programs

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Konzepte objektorientierter Programmierung

Basic Object-Oriented Programming in Java

Transcription:

Introduction to Object-Oriented Programming Objects and classes Encapsulation and information hiding Mental exercises Classification and exemplification Aggregation and decomposition Generalization and specialization Inheritance Polymorphism and dynamic binding Java an example of an object-oriented programming language Program example History of Java Comparison to C/C+ OOP: Introduction 1

Objects and Classes Mammal Two-legs Very large brains Omnivorous (plants + meat) Mammal Tusks Four legs Herbivorous (plant eater) OOP: Introduction 2

The Object Concept An object is an encapsulation of data. An object has identity (a unique reference) social security number (cpr), employee number, passport number state, also called characteristics (variables) hungry, sad, drunk, running, alive behavior (methods) eat, drink, wave, smile, kiss An object is an instance of an class. A class is often called an Abstract Data Type (ADT). OOP: Introduction 3

The Class Concept A class is a collection of objects (or values) and a corresponding set of methods. A class encapsulates the data representation and makes data access possible at a higher level of abstraction. Example 1: A set of vehicles with operations for starting, stopping, driving, get km/liter, etc. Example 2: A time interval, start time, end time, duration, overlapping intervals, etc. Example 3: A string, upper case, compare, lower case, etc. str.equals(otherstr) class/java style strcmp(str, otherstr) C style OOP: Introduction 4

Encapsulation and Information Hiding Data can be encapsulated such that it is invisible to the outside world. Data can only be accessed via methods. Data Function Function Function send message Data Method Method Method Procedural Class OOP: Introduction 5

Encapsulation and Information Hiding, cont. What the outside world cannot see it cannot depend on! The object is a fire-wall between the object and the outside world. The hidden data and methods can be changed without affecting the outside world. Outside world Client interface An object Visible data and methods Hidden (or encapsulated) data and methods OOP: Introduction 6

Class vs. Object Class A description of the common properties of a set of objects. A concept. A class is a part of a program. Example 1: Person Example 2: Album Object A representation of the properties of a single instance. A phenomenon. An object is part of data and a program execution. Example 1: Bill Clinton, Bono, Viggo Jensen. Example 2: A Hard Day's Night, Joshua Tree, Rickie Lee Jones. OOP: Introduction 7

Connection between Object and Class In object-oriented programming we write classes The text files we create contain classes! Static One Objects are created from classes A class contains a receipe on how to make objects Dynamic Many Ingrediens 250 g digestive biscuits food processor 125 g soft brown sugar saucepan 125 g butter wooden spoon 50 g raisins 18 cm sandwich tin (greased) 3 tablespoons cocoa powder fridge 1 egg, beaten knife 25 g = 1 oz 2.5 cm = 1 inch Process blend bake source http://www.filflora.com OOP: Introduction source http://www.icbl.hw.ac.uk/ltdi/cookbook/chocolate_cake/ 8

Type and Interface An object has type and an interface. Account balance() withdraw() deposit() Type Interface To get an object To send a message Account a = new Account() Account b = new Account() a.withdraw() b.deposit() a.balance() OOP: Introduction 9

Instantiating Classes An instantiation is a mechanism where objects are created from a class. Always involves storage allocation for the object. A mechanism where objects are given an initial state. Static Instantiating In the declaration part of a program. A static instance is implicitly created Dynamic Instantiating In the method part of a program. A dynamic instance is created explicitly with a special command. OOP: Introduction 10

Interaction between Objects Interaction between objects happens by messages being send. A message activates a method on the calling object. An object O1 interacts with another object O2 by calling a method on O2 (must be part of the client interface). O1 sends O2 a message O1 and O2 must be related to communicate. The call of a method corresponds to a function (or procedure) call in a non-object-oriented language such as C or Pascal. O1 message message O3 O2 message OOP: Introduction 11

Phenomenon and Concept A phenomenon is a thing in the real world that has individual existence. an object A concept is a generalization, derived from a set of phenomena and based on the common properties of these phenomena. a class Characteristics of a concept A name Intension, the set of properties of the phenomenon Extension, the set of phenomena covered by the concept. OOP: Introduction 12

Classification and Exemplification, Examples hat, 23, 34, mouse, telephone, book, 98, 45.34, hello numbers: 23, 34, 98, 45.34 words: hat, mouse, telephone, book, hello mouse, tyrannosaurus rex, allosaurus, elephant, velociraptor dinosaur: tyrannosaurus rex, allosaurus, velociraptor mammal: mouse, elephant OOP: Introduction 13

Classification and Exemplification, cont. A classification is a description of which phenomena that belongs to a concept. An exemplification is a phenomenon that covers the concept Concept classification exemplification Phenomenon OOP: Introduction 14

Aggregation and Decomposition, Example Idea: make new objects by combining existing objects. Reusing the implementation! Engine start() stop() Gearbox up() down() Door open() close() existing classes Aggregation Car Engine Gearbox Doors[4] start() drive() new class Car has-a Gearbox and Car has-an Engine OOP: Introduction 15

Aggregation and Decomposition An aggregation consists of a number of (sub-)concepts which collectively is considered a new concept. A decomposition splits a single concept into a number of (sub-)concepts. Concept Concept Concept Concept decomposition aggregation Concept Concept Concept Concept OOP: Introduction 16

Generalization and Specialization OOP: Introduction source : www.geology.ucdavis.edu/ ~GEL12/dinosauria.Html 17

Generalization and Specialization, cont. Generalization creates a concept with a broader scope. Specialization creates a concept with a narrower scope. Reusing the interface! Concept A specialization Concept B Concept C generalization Concept D Vehicle Car Hatchback Station car Sedan Truck Pickup OOP: Introduction 18

Generalization and Specialization, Example Inheritance: get the interface from the general class. Objects related by inheritance are all of the same type. Shape draw() resize() Circle draw() resize() Line draw() resize() Rectangle draw() resize() Square draw() resize() Square is-a Shape or Square is-like-a Shape OOP: Introduction 19

Generalization and Specialization in Java Object clone() equals() tostring()... Shape draw() resize() Circle Line Rectangle OOP: Introduction 20

Polymorphism and Dynamic Binding void dosomething(shape s){ s.draw(); // magically calls the specific class s.resize(); } Circle c = new Circle(); Line l = new Line(); Rectangle r = new Rectangle(); dosomething(c); dosomething(l); dosomething(r); // dynamic binding Polymorphism: One piece of code works with all shape objects. Dynamic binding: How polymorphism is implemented. OOP: Introduction 21

Benefit Generalization and Specialization Take previous Shape class hierarchy remove inheritance remove general and abstract class Shape Rectangle draw() resize() Square draw() resize() Circle draw() resize() Line draw() resize() OOP: Introduction 22

Code Example, Revisited void dosomething(circle c){ c.draw(); c.resize(); } void dosomething(line l){ l.draw(); l.resize(); } void dosomething(rectangle r){ r.draw(); r.resize(); } void dosomething(square s){ s.draw(); s.resize(); } Circle c = new Circle(); Line l = new Line(); Rectangle r = new Rectangle(); dosomething(c); dosomething(l); dosomething(r); Similar code is repeated OOP: Introduction 23

Java Program Structure // comment on the class public class MyProg { String s = Viggo ; variable } /** * The main method (comment on method) */ public static void main (String[] args){ // just write some stuff System.out.println ("Hello World"); } method header method body OOP: Introduction 24

Java Class Example Car /** A simple class modeling a car. */ public class Car { // instance variables private String make; private String model; private double price; // constructor public Car(String m, String mo, double p) { make = m; model = mo; price = p; } // string representation of the car public String tostring() { return "make: " + make + " model: " + model + " price: " + price; } } OOP: Introduction 25

Byte Code vs. Executable MyProg.java MyProg.cpp javac MyProg.java Java Class File MyProg.class Portable Byte Code Java Virtual Machine Operating System gcc MyProg.cpp -o myprog.exe Executable myprog.exe Operating System Java/C# world C++ world OOP: Introduction 26

History of Java 1990 Oak (interactive television, big failure) 1994 Java (for the Internet) Main feature: "Write Once, Run Any Where" => wrap the operating system so they all look the same Designed for A fresh start (no backward compatibility) Pure OOP: C++ Syntax, Smalltalk style Improvements over C++ much harder to write a bad program Internet programming Very hard to create a virus Run in a web browser (and at the server) There is a speed issue (from Java 1.3 and up much better) C# Microsoft's Java-Killer project release 2001 Language very similar to Java Commen-Language Runtime (CLR) supports 30+ languages OOP: Introduction 27

Difference from C/C++ Everything resides in a class variables and methods Garbage collection bye bye malloc(), free(), and sizeof() Error and exception handling handling No global variables or methods No local static variables No separation of declaration and implementation Bye bye header files No explicit pointer operations (uses references) No preprocessor (but something similar) Has fewer dark corners Has a much larger standard library (Java Developer Kit or JDK) OOP: Introduction 28

Summary Classes are recipes for creating objects All objects are instances of classes Encapsulation Key feature of object-oriented programming Separation of interface from implementation It is not possible to access the hidden/encapsulated parts of an object Aggregation and decomposition has-a relationship Generalization and specialization (inheritance) is-a or is-like-a relationship Polymorpishm/dynamic binding Softening static typing OOP: Introduction 29

Common Mistakes and Errors // what is ugly here? public class main { public static void main(string[] args){ System.out.println( Hello World );} } // what is wrong here? public class MyClass { public void static main(string[] args){ system.out.println( Hello World );} } // what is ugly here? public class MyClass { public static void main(string[] args){ System.out.println( Hello World );} }; OOP: Introduction 30

Structuring by Program or Data? What are the actions of the program vs. which data does the program act on. Top-down: Stepwise program refinement Bottom-up: Focus on the stable data parts then add methods Object-oriented programming is bottom-up. Programs are structure with outset in the data. C and Pascal programs are typically implemented in a more top-down fashion. OOP: Introduction 31

Pure Object-Oriented Languages Five rules [source: Alan Kay] Everything in an object. A program is a set of objects telling each other what to do by sending messages. Each object has its own memory (made up by other objects). Every object has a type. All objects of a specific type can receive the same messages. Java breaks some of these rules in the name of efficiency. OOP: Introduction 32