Introduction to Object-Oriented Programming

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Objects, Classes, and Packages “Static” Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in.
1 CS 171: Introduction to Computer Science II Review: OO, Inheritance, and Libraries Ymir Vigfusson.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 – Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To.
1 Classes Object-oriented programming: Model the problem as a collection of objects that have certain attributes and interact with one another and/or the.
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 2 – An Introduction to Objects and Classes Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
The Java Programming Language  Simple – but abstract  Safe  Platform-independent ("write once, run anywhere")  Has a Rich growing library  Designed.
CHAPTER 2 OBJECTS AND CLASSES Goals: To understand the concepts of classes and objects To realize the difference between objects and object references.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
Chapter 3 Implementing Classes. Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand.
ACM/JETT Workshop - August 4-5, Object-Oriented Basics & Design.
Defining Classes and Methods Chapter 4.1. Key Features of Objects An object has identity (it acts as a single whole). An object has state (it has various.
Java Programming Review (Part I) Enterprise Systems Programming.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Object-Oriented Programming
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Chapter 3 Implementing Classes. Assignment Read 3.1 – 3.5 and take notes complete Self Check Exercises 1-10; Due September 24 th Read 3.6 – 3.8 and take.
Week 4 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Introduction to Java Java Translation Program Structure
Chapter 4 Introduction to Classes, Objects, Methods and strings
Class Relationships and Object Interaction CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila.
Object Oriented Programing (OOP)
ACM/JETT Workshop - August 4-5, : Defining Classes in Java.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
Programming Languages and Paradigms Activation Records in Java.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Methods.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 3 Implementing Classes
Lecture 3 John Woodward.
Classes and OOP.
Yanal Alahmad Java Workshop Yanal Alahmad
Classes Object-oriented programming: Example: Bank transactions
Implementing Classes Yonglei Tao.
suggested reading: Java Ch. 6
Chapter Goals To become familiar with the process of implementing classes To be able to implement and test simple methods To understand the purpose and.
Chapter Three - Implementing Classes
What is an object? An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the.
Java Enter your code from FRQ to Shell
You can work in groups on this program.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Chapter 3 Implementing Classes
More on Classes and Objects
Introduction to Objects
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
JAVA CLASSES.
AN INTRODUCTION TO OBJECTS AND CLASSES
Classes CS 21a: Introduction to Computing I
Java 1/31/2017 Back to Objects.
Dr. R Z Khan Handout-3 Classes
Methods/Functions.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Introduction to Object-Oriented Programming Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008

Object-Oriented Programming The traditional definition of a program is: a sequence of instructions to be executed on a computer In the Object-Oriented Programming (OOP) paradigm, a program that executes is a collection of interacting objects In this paradigm, the programs we write/specify what are in these objects and how these objects behave Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming What is an Object? Definition: A “thing” that has type, identity, state, and behavior type: belongs to a class of similar objects identity: is a distinct instance of that class state / attributes: has a set of properties (or fields) each field has a value, that may be different per object of the same class behavior: what the object knows how to do objects of the same class can carry out the same behavior (methods) Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Examples of Objects state/attributes # of gallons of gas in tank total # of miles run so far efficiency (mpg) behavior drive load gas change efficiency check gas check odometer reading state/attributes on (true or false) behavior switch on switch off check if on LightBulb Car state/attributes balance behavior deposit withdraw check balance Note each object is an “instance” of that “type” of object each instance has its own values for its attributes e.g., different accounts can have different balances BankAccount Introduction to Object-Oriented Programming

BankAccount example (A Preview) public class BankAccount { private double balance = 0; public double getBalance() return balance; } public void deposit( double amount ) balance = balance + amount; // … more code follows BankAccount.java BankAccount state/attributes balance behavior get balance deposit withdraw Introduction to Object-Oriented Programming

Class Definition in Java BankAccount public class BankAccount { private double balance = 0; public double getBalance() return balance; } public void deposit( double amount ) balance = balance + amount; // … more code follows BankAccount.java type (or class) state/attributes (fields) behavior (methods) may have input parameters in parenthesis may have output (or “return”) type has “body” with code Introduction to Object-Oriented Programming

A Class with a Constructor public class BankAccount { private double balance; public BankAccount() balance = 0; } public double getBalance() return balance; public void deposit( double amount ) balance = balance + amount; // … more code follows BankAccount.java Constructor: special method that initializes the object For now, view constructors as an alternative to initializing fields as they are declared Later: more advanced uses for constructors Introduction to Object-Oriented Programming

A Class and Its Instances A single class can have multiple instances Each instance is a separate object Each instance can have different values for its fields The definition of methods is the same for all instances of the same type Thus, there is only one class definition Written as the .java file for that class Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming BlueJ Demo Create and compile a BankAccount class (BankAccount.java => BankAccount.class) Create BankAccount objects (instances of the class) Right-click on the class Carry out operations on the BankAccount objects (invoke the deposit and getBalance methods) Right-click on the instances Introduction to Object-Oriented Programming

Using BankAccount objects in a separate Java program Instantiate and use a BankAccount object in a separate Java application How? Within the same Java project, create, compile and execute BankAccountTester.java with a public static void main(String args[]) method, containing the following code… Introduction to Object-Oriented Programming

Java code using a BankAccount object BankAccount myAccount; // declare variable myAccount = new BankAccount(); // create instance myAccount.deposit( 1000 ); // call a method myAccount.deposit( 500 ); // call it again System.out.print( “My balance is now ” ); System.out.println( myAccount.getBalance() ); /* We expect 1500 to be printed by the last statement */ Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Summary In Java, we write programs for objects These programs are called classes A class consists of fields and methods to specify the state and behavior for its objects Once a class has been defined, objects of that class can be created (instantiated) Methods are invoked on an object, and may cause the state of the object to change Introduction to Object-Oriented Programming