Introduction to ObjectOriented Programming Notes Supplement CS 1054
Introduction to Object-Oriented Programming Notes Supplement CS 1054 Introduction to Programming in Java Spring 2008 6/22/2005
Object-Oriented Programming n n n 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 2
What is an Object? Definition: A “thing” that has type, identity, state, and behavior n n n 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) n n each field has a value, that may be different per object of the same class behavior: what the object knows how to do n objects of the same class can carry out the same behavior (methods) Introduction to Object-Oriented Programming 3
Examples of Objects n n on (true or false) n behavior n n Light. Bulb n state/attributes n n switch off check if on state/attributes n behavior n n Car n n n state/attributes n n behavior n Bank. Account balance n n deposit withdraw check balance # of gallons of gas in tank total # of miles run so far efficiency (mpg) n drive load gas change efficiency check gas check odometer reading Note n each object is an “instance” of that “type” of object n each instance has its own values for its attributes n Introduction to Object-Oriented Programming e. g. , different accounts can have different balances 4
Bank. Account example (A Preview) public class Bank. Account { private double balance = 0; public double get. Balance() { return balance; } Bank. Account n state/attributes n n public void deposit( double amount ) { balance = balance + amount; } // … more code follows balance behavior n n n get balance deposit withdraw Bank. Account. java } Introduction to Object-Oriented Programming 5
Class Definition in Java Bank. Account n type (or class) n state/attributes (fields) n behavior (methods) n n n may have input parameters in parenthesis may have output (or “return”) type has “body” with code public class Bank. Account { private double balance = 0; Bank. Account. java public double get. Balance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } // … more code follows } Introduction to Object-Oriented Programming 6
A Class with a Constructor n n n 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 public class Bank. Account { private double balance; Bank. Account. java public Bank. Account() { balance = 0; } public double get. Balance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } // … more code follows } Introduction to Object-Oriented Programming 7
A Class and Its Instances n A single class can have multiple instances n n 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 n Written as the. java file for that class Introduction to Object-Oriented Programming 8
Blue. J Demo n n Create and compile a Bank. Account class (Bank. Account. java => Bank. Account. class) Create Bank. Account objects (instances of the class) n n Right-click on the class Carry out operations on the Bank. Account objects (invoke the deposit and get. Balance methods) n Right-click on the instances Introduction to Object-Oriented Programming 9
Using Bank. Account objects in a separate Java program n n Instantiate and use a Bank. Account object in a separate Java application How? Within the same Java project, create, compile and execute Bank. Account. Tester. java n with a public static void main(String args[]) method, containing the following code… Introduction to Object-Oriented Programming 10
Java code using a Bank. Account object Bank. Account my. Account; // declare variable my. Account = new Bank. Account(); // create instance my. Account. deposit( 1000 ); // call a method my. Account. deposit( 500 ); // call it again System. out. print( “My balance is now ” ); System. out. println( my. Account. get. Balance() ); /* We expect 1500 to be printed by the last statement */ Introduction to Object-Oriented Programming 11
Summary n n n 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 12
- Slides: 12