Introduction to Object-Oriented Programming - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Introduction to Object-Oriented Programming

Description:

state/attributes # of gallons of gas in tank. total # of miles run so far. efficiency (mpg) ... on an object, and may cause the state of the object to change ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 13
Provided by: luisf55
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Object-Oriented Programming


1
Introduction toObject-Oriented Programming
  • Notes Supplement
  • CS 1054 Introduction to Programming in Java
  • Spring 2008

2
Object-Oriented Programming
  • The traditional definition of a program isa
    sequence of instructions to be executedon 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

3
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)

4
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

BankAccount
5
BankAccount example(A Preview)
BankAccount
  • state/attributes
  • balance
  • behavior
  • get balance
  • deposit
  • withdraw

6
Class Definition in Java
BankAccount
  • type (or class)
  • state/attributes (fields)
  • behavior (methods)
  • may have input parameters in parenthesis
  • may have output (or return) type
  • has body with code

7
A Class with a Constructor
  • 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

8
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

9
BlueJ Demo
  • Create and compile a BankAccount class
    (BankAccount.java gt 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

10
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 apublic static void main(String args)
    method, containing the following code

11
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 /

12
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
Write a Comment
User Comments (0)
About PowerShow.com