CS2011 Introduction to Programming I Objects and Classes Chengyu Sun California State University, Los Angeles
Represent Data To process data using computers, we first need to represent the data in a programming language 10 int 3.1415 double "hello" String 90, 100, 95 int[] 90, 100, 95 85, 90, 100 79, 80, 90 int[][]
Example: Banking System Create a banking system that helps a bank to manage customers and accounts, e.g. open an account, get the balance of an account, deposit, withdraw, transfer, and so on How do we represent customers and accounts??
What Information Do We Need to Keep? Customer Account name String id int address String balance double owner ??
One Way to Implement a Bank String[] customerNames; String[] customerAddresses; int[] accountIds; double[] accountBalances; String[] accountOwnerNames;
A Better Way to Implement A Bank class Customer { String name; String address; } class Account { int id; double balance; Customer owner; } Customer[] customer; Account[] accounts;
The Object-Oriented Paradigm … Computer programs are usually created to facilitate real-world tasks A good programming language should make it easier to mimic (i.e. model) real-world matters
… The Object-Oriented Paradigm The world consists of objects Each object has some attributes, and is often associated with some operations The same type of objects share the same set of attributes and/or operations
From Concept to Code Type Class Attributes Variables Operations Methods Objects in the real world Objects in programs
Class in Java Class name Access modifier Fields, a.k.a. instance variables public class Customer { String name; String address; public Customer(String cname, String caddress) { name = cname; address = caddress; } Constructor
About Constructor A constructor is a special method: no return type, and same name as class Constructors are used to create new objects (often called instances) of a class A class must have at least one constructor If a class doesn’t have a constructor, JVM will automatically creates one for the class, known as the default constructor
Create and Access An Object Customer c = new Customer("John", "123 Main St"); Use the new keyword like when creating an array Call one of the constructors System.out.println( c.name ); Use the . operator to access object fields and methods
Class As Type A class can be considered a user-defined type Type Customer c = new Customer("John", "123 Main St"); Type Customer[] customers = new Customer[100];
Banking System: Data A bank manages a number of customers and accounts Use arrays to store customers and accounts Use counts to keep track the actual number of customers and accounts
Classes Are More Than Just Data Example: add account operations like deposit, withdraw, and get balance
static vs Non-static public class Foo { static int a = 0; int b; Foo() { b = 0; } public void inc() { ++a; ++b; } public void print() System.out.println(a); System.out.println(b); A static member of a class is shared by all objects of the class Foo f1 = new Foo(); Foo f2 = new Foo(); f1.print(); f2.print(); // ?? f1.inc(); f1.print(); // ?? f2.inc(); f2.print(); // ??
Return An Object Example: open an account in a bank (name, address, initial deposit) Just like arrays, objects are created on the heap Returning an object means returning the reference (i.e. the address) of the object
Pass Object to Method Just like arrays, object parameters are pass-by-reference Example: transfer money from one account to another
Keyword this A reference to an object itself De-shadowing A reference to a constructor public class Foo { int x; public Foo( int x ) { this.x = x; } public Foo() { this(-1); The parameter x shadows the field x, meaning x inside the method refers to the parameter instead of the field
More About OO Programming Encapsulation Inheritance Polymorphism
Readings Chapter 9 of the textbook