Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Object-Oriented Programming

Similar presentations


Presentation on theme: "Introduction to Object-Oriented Programming"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

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 Introduction to Object-Oriented Programming

9 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

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 a public static void main(String args[]) method, containing the following code… Introduction to Object-Oriented Programming

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 */ Introduction to Object-Oriented Programming

12 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


Download ppt "Introduction to Object-Oriented Programming"

Similar presentations


Ads by Google