Download presentation
1
Introduction to Object-Oriented Programming
CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University
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 specify what are in these objects and how these objects behave Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
3
So … What is an Object? A “thing” that has type, identity, state, and behavior type: belongs to a “class” of similar objects identity: is a distinct “instance” of a class of objects state / attributes: has a set of properties (aka fields) each field can have different values behavior: has “methods” (things that the object knows how to do) we say we “call” a method on the object Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
4
Examples of Objects LightBulb Car BankAccount state/attributes
# of liters of gas in tank total # of km run so far efficiency (km/liter) 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 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
5
BankAccount example (A Preview)
state/attributes balance behavior get balance deposit withdraw public class BankAccount { private double balance = 0; public double getBalance() return balance; } public void deposit( double amount ) balance = balance + amount; … BankAccount.java BankAccount BankAccount double balance double getBalance() void deposit( double amount ) … and more Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
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; … 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 “double” means a floating Point number like Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
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; … BankAccount.java Constructor: special method that handles initialization For now, view constructors as an alternative to initializing fields as they are declared Later: more advanced uses for constructors Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
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 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
9
Lab Exercise: Try it in BlueJ
Create and compile a BankAccount class (BankAccount.java) 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 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
10
Lab Exercise, continued
Instantiate and use a BankAccount object in a Java application (use the command prompt) How? In the same folder containing the BankAccount class, create BankSystem.java Inside the public static void main(String args[]) method of BankSystem.java, type the following code… Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
11
Lab Exercise, continued
public static void main( String args[] ) { BankAccount b = new BankAccount(); b.deposit( ); b.withdraw( ); System.out.println( b.getBalance() ); b.deposit( ); } Compile and execute BankSystem.java The values and should be printed out Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
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 Copyright 2008, by the authors of these slides, and Ateneo de Manila University. All rights reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.