Object Oriented Programming CSC 171 FALL 2001 LECTURE 11.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
Ch 12: Object-Oriented Analysis
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Classes. Object-Oriented Design Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo, a gradebook.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Oriented Design CSC 171 FALL 2001 LECTURE 12.
Class Design CSC 171 FALL 2004 LECTURE 11. READING Read Chapter 7 It’s abstract But it should help with project #1.
ECE122 L6: Problem Definition and Implementation February 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 6 Problem Definition and Implementation.
1 Classes and Objects Overview l Classes and Objects l Constructors l Implicit Constructors l Overloading methods this keyword l public, private and protected.
Classes, Encapsulation, Methods and Constructors
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Object Oriented Concepts in Java Objects Inheritance Encapsulation Polymorphism.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Using Objects Object: an entity in your program that you can manipulate Attributes Methods Method: consists of a sequence of instructions that can access.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
C++ fundamentals.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
CSS446 Spring 2014 Nan Wang.  To learn how to choose appropriate classes for a given problem  To understand the concept of cohesion  To minimize dependencies.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Classes All Java code must be inside classes Class types – Utility classes Used to store constants, utility methods, etc. – Driver classes – Abstract Data.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
(c) University of Washington01-1 CSC 143 Java Programming as Modeling Reading: Ch. 1-6.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
 Sometimes a new class is a special case of the concept represented by another ◦ A SavingsAccount is-a BankAccount ◦ An Employee is-a Person  Can extend.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 3: An Introduction to Classes 1 Chapter 3 An Introduction to Classes.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
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.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Classes (Part 1) Lecture 3
Lecture 3 John Woodward.
Classes and OOP.
Implementing Classes Yonglei Tao.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Packages, Interfaces & Exception Handling
Chapter Three - Implementing Classes
د.سناء الصايغ الفصل الأول البرمجة الشيئية
Interfaces in Java.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
JAVA CLASSES.
By Rajanikanth B OOP Concepts By Rajanikanth B
Classes.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Object Oriented Programming CSC 171 FALL 2001 LECTURE 11

History: ENIAC Electronic Numerical Integrator and Computer 1943 – 1947 Work on ENIAC at the Univ. of Pennsylvania John Mauchly & J. Presper Eckert. The world's first electronic digital computer was developed to compute World War II ballistic firing tables.

Computer Science What is computer science?

Computer Science “fundamentally, computer science is the science of abstraction – creating the right model for a problem and devising the appropriate mechanizable technique to solve it.” - A. Aho and J. Ullman

Acts of mind - Locke “The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three:

Aggregation 1. Combining several simple ideas into one compound one, and thus all complex ideas are made

Comparision 2. The second is brining two ideas, where simple or complex, together, and setting them by one another so as to take aview of them at once,without uniting them into one, by which it gets all its ideas of relations.

Abstraction 3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all general ideas are made.” John Locke, An Essay Concerning Human Understanding, (1690)

Object Orientation Objects are the means of aggregation and abstraction, in an OO programming language Aggregation – bundling data elements Abstraction - inheritance

Data Encapulation OOP encapulates – data – behavior

Interfaces We define systems in terms of many objects – Each type of object has certain behaviors methods – Each object has certain data Instance variables Objects communicate with other via well defined interfaces

Information Hiding When we design objects which communicate exclusively via interfaces - we insulate (abstract) the outward functionality from the inward implementation. This insulation is the principle of information hiding – the inward details of the implementation are hidden from the outside objects

Benefits of Information Hiding Information hiding promotes program modifiability. Clients are not required to know the internals of a class in order to use it. So, if the class changes inside, then the client need not be changed The client and the object are said to be “loosely coupled”

Example – A bank account

A specific instance

Multiple instances

Bank Account - behaviors public class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance){ balance = initialBalance; } public void deposit(double amount){ balance = balance + amount; } public void withdraw(double amount){ balance = balance - amount; } public double getBalance(){return balance;} }

this Java conserves storage by maintaining only one copy of each method per class The same method is invoked by every object Every object has its own copy of its instance variables Every object, by default has a reference to itself – “this” is the name of every object’s reference to itself

Bank Account - this public class BankAccount { private double balance; public BankAccount() { this.balance = 0; } public BankAccount(double balance){ this.balance = balance; } public void deposit(double amount){ this.balance = this.balance + amount; } public void withdraw(double amount){ this.balance = this.balance - amount; } public double getBalance(){ return this.balance;} }

Sometimes, classes share Consider a variation of the BankAccount public class BankAccount {.... private double balance; private int accountNumber; // we want to assign sequential numbers }

Sharing data between objects of the same class We want to set the account number automatically public class BankAccount { private double balance; private int accountNumber; private int lastAssignedNumber = 0; //Will this work? public BankAccount() { lastAssignedNumber++; accountNumber = lastAssignedNumber; //??????? }

Static/class variables We don’t want each instance of the class to have its own value for lastAssignedNumber We need to have a single value that is the same for the entire class. These are called class or static variables

Sharing data between objects of the same class We want to set the account number automatically public class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber = 0; public BankAccount() { lastAssignedNumber++; accountNumber = lastAssignedNumber; }

Finally Some types of variables are fixed constants, that we do not want to change Like conversion factors We can use the keyword “final” to prevent changes So we have a “constant variable” Sort of like – “jumbo shrimp” – “freezer burn”

Classes share constants Consider a variation of the BankAccount public class Converter { private final static double miles2km = 0.6; }