oop: classes and objects

24
http://atit.patumvan.com Object-Oriented Programming: Classes and Objects in Java Atit Patumvan Faculty of Management and Information Sciences Naresuan University

Upload: atit-patumvan

Post on 10-May-2015

1.160 views

Category:

Entertainment & Humor


8 download

TRANSCRIPT

Page 1: OOP: Classes and Objects

http://atit.patumvan.com

Object-Oriented Programming:Classes and Objects in Java

Atit PatumvanFaculty of Management and Information SciencesNaresuan University

Page 2: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

2

http://atit.patumvan.com

Hello, World!!

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello, World!!"); }}

public class HelloWorld {

public static void main(String[] args) { System.out.println("Hello, World!!"); }}

public class HelloWorld {

public HelloWorld(){ System.out.println("Hello, World!!"); }

public static void main(String[] args) { new HelloWorld(); }}

public class HelloWorld {

public HelloWorld(){ System.out.println("Hello, World!!"); }

public static void main(String[] args) { new HelloWorld(); }}

Hello, World!!

Page 3: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

3

http://atit.patumvan.com

Class Definition+ BankAccount

-number : long-balance : double

+deposit(amount : double) : void+withdraw(amount : double) : void

+ Customer

-name : String+ssn : String

holder

0 1

public class BankAccount {

private long number; private double balance; public Customer holder;

public void deposit(double amount) { balance += amount; }

public void withdraw(double amount) { if (balance >= amount) { balance -= amount; } else { System.out.println("System cannot process this transaction."); } }}

Properties

MethodsMethods

Class

Page 4: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

4

http://atit.patumvan.com

Object Declaration

account BankAccount

A Class defines a data type that can be used to declare variables

int number;BankAccount account;

number int

Page 5: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

5

http://atit.patumvan.com

Object Creation

account1

0x156F4H

BankAccount

● Objects are created with the new operator

● To create an object means to allocate memory space for variables

● New allocate memory for an object and return a reference to the object

Int number;BankAccount account1;

number = 20;account1 = new BankAccount();

BankAccount account2 = new BankAccount();

number int

10

0x156F4H

account2

0x15FFFH

BankAccount0x15FFFH

Page 6: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

6

http://atit.patumvan.com

Access to Properties

BankAccount account;Customer customer;

account = new BankAccount();customer = new Customer(); customer.name = “Bob Goodman”;customer.ssn = “2567893”;

account.number = 23421;account.balance = 563948.50;account.holder = customer;

System.out.println(“Owner: ”+account.holder.name);System.out.println(“Balance:”+account.balance);

customerCustomer

customerCustomer

number

23421

long

ssn

“2567893”

String

balance

563948.50

holder

double

name

“Bob Goodman”

String

Page 7: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

7

http://atit.patumvan.com

Methods

● Method are functions defined within a class

● Methods can directly reference the variables of the class

● Methods can only be invoked on object of the class to which they belong

● During the execution of a method, invoked upon an object of class, the variables in the class take the value they have in object

account2.deposit(1000);

account2.deposit(1000); private double balance;

public void deposit(double amount) { balance += amount; }

private double balance;

public void deposit(double amount) { balance += amount; }

account2.balance

Page 8: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

8

http://atit.patumvan.com

Method Calls from within a Method

Method can directly invoke any other method of same classpublic class BankAccount { : public void withdraw(double amount) { : }

public void transfer(BankAccount target, double amount) { if (balance >= amount) { withdraw(amount); target.deposit(amount); } }}

public class BankAccount { : public void withdraw(double amount) { : }

public void transfer(BankAccount target, double amount) { if (balance >= amount) { withdraw(amount); target.deposit(amount); } }}

Page 9: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

9

http://atit.patumvan.com

Method Declaration

public static double cubeVolume(double sideLength){ double volume = sideLength*sideLength*sideLength; return volume; }

Name of parameter variable

Type of parameter variableType of return value

Name of method

Method body

Page 10: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

10

http://atit.patumvan.com

length

2

sideLength

2

parameter passing (copy value)

volume

82 * 2 * 2 → 8

result

8Return result (copy value)

double length = 2;double result = cubeVolume(length);

double length = 2;double result = cubeVolume(length);

public static double cubeVolume(double sideLength){ double volume = sideLength*sideLength*sideLength; return volume; }

public static double cubeVolume(double sideLength){ double volume = sideLength*sideLength*sideLength; return volume; }

Parameter Passing and Return Value

Page 11: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

11

http://atit.patumvan.com

Variable Scope

Class X

Method Y

Method Z Branch Z1 int f;

int a;

int b;

int c;

branch Y1

branch Y2

int d;

int e;

The scope of variable is the part of program in which you access itpublic class X { int a; public void Y() { int b; If (...){ // branch Y1 int d; } else { // branch Y2 int e; } }

public void Z () { int c; If (...) { // branch Z1 int f; } }}

public class X { int a; public void Y() { int b; If (...){ // branch Y1 int d; } else { // branch Y2 int e; } }

public void Z () { int c; If (...) { // branch Z1 int f; } }}

Page 12: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

12

http://atit.patumvan.com

Accessors and Mutators Method

public class Customer {

private String name; private String ssn; : public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getSsn() { return ssn; }

public void setSsn(String ssn) { this.ssn = ssn; } :}

public class Customer {

private String name; private String ssn; : public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getSsn() { return ssn; }

public void setSsn(String ssn) { this.ssn = ssn; } :}

● Accessors is a method with in a class designed to retrieve the value of instance variable in that same class (get method)

● Accessors is a method with in a class designed to change the value of instance variable in that same class (set method)

Page 13: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

13

http://atit.patumvan.com

Constructor

● “Methods” that are execute automatically when the object of class are created

● Typical purpose● Initial values for the object of variables● Other initialization operation

● Advantage● Syntax simplification● Encapsulation of object variables: avoid external access

Page 14: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

14

http://atit.patumvan.com

Constructor Example

public class BankAccount { : public BankAccount(long num, Customer cus, double bal) { number = num; holder = cus; balance = bal; } :}

public class BankAccount { : public BankAccount(long num, Customer cus, double bal) { number = num; holder = cus; balance = bal; } :}

public class Tester {

public static void main(String[] args) { BankAccount account; Customer customer;

customer = new Customer("Bob Goodman", "2567893"); account = new BankAccount(23421, customer, 563948.50);

}}

public class Tester {

public static void main(String[] args) { BankAccount account; Customer customer;

customer = new Customer("Bob Goodman", "2567893"); account = new BankAccount(23421, customer, 563948.50);

}}

public class Customer {

private String name; private String ssn;

public Customer(String name, String ssn) { this.name = name; this.ssn = ssn; }}

public class Customer {

private String name; private String ssn;

public Customer(String name, String ssn) { this.name = name; this.ssn = ssn; }}

Page 15: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

15

http://atit.patumvan.com

Default Constructor

● If no constructors are defined, Java defines one by default

● If a constructor is explicitly defined, The default constructor is not defined

public class Customer {

private String name; private String ssn;

public Customer() { }}

public class Customer {

private String name; private String ssn;

public Customer() { }}

public class Customer { : public Customer(String name, String ssn) { : }}

public class Customer { : public Customer(String name, String ssn) { : }}

Page 16: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

16

http://atit.patumvan.com

The this Variable

● Implicitly defined in the body of all methods

● Reference to the object on which the method invoked

public class Customer {

private String name; private String ssn;

public Customer(String name, String ssn) { this.name = name; this.ssn = ssn; }}

public class Customer {

private String name; private String ssn;

public Customer(String name, String ssn) { this.name = name; this.ssn = ssn; }}

Page 17: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

17

http://atit.patumvan.com

this in Constructors

public class BankAccount { : public BankAccount(long num, Customer cus, double bal) { number = num; holder = cus; balance = bal; cus.newAccount(this); } :}

public class BankAccount { : public BankAccount(long num, Customer cus, double bal) { number = num; holder = cus; balance = bal; cus.newAccount(this); } :}

public class Customer { : private BankAccount accounts[] = new BankAccount[20]; int nAccounts = 0;

public void newAccount(BankAccount account) { accounts[nAccounts++] = account; } :}

public class Customer { : private BankAccount accounts[] = new BankAccount[20]; int nAccounts = 0;

public void newAccount(BankAccount account) { accounts[nAccounts++] = account; } :}

Customer customer;

customer = new Customer("Bob Goodman", "2567893");

new BankAccount(23421, customer, 563948.50);new BankAccount(23421, customer, 789652.50);

Customer customer;

customer = new Customer("Bob Goodman", "2567893");

new BankAccount(23421, customer, 563948.50);new BankAccount(23421, customer, 789652.50);

Page 18: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

18

http://atit.patumvan.com

Overloading

: public BankAccount(long num, Customer cus) { number = num; holder = cus; cus.newAccount(this); } public BankAccount(long num, Customer cus, double bal) { number = num; holder = cus; balance = bal; cus.newAccount(this); } public void deposit(int amount) { balance += amount; }

public void deposit(double amount) { balance += amount; } :

: public BankAccount(long num, Customer cus) { number = num; holder = cus; cus.newAccount(this); } public BankAccount(long num, Customer cus, double bal) { number = num; holder = cus; balance = bal; cus.newAccount(this); } public void deposit(int amount) { balance += amount; }

public void deposit(double amount) { balance += amount; } :

Constructor Overloading

Method Overloading

Page 19: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

19

http://atit.patumvan.com

Packages

● Set of classes defined in a folder

● Avoid symbol conflicts

● Each class belongs to package

● If no package is defined for a class, java includes it in the default package

Page 20: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

20

http://atit.patumvan.com

Define Package

package graphics;

public class Circle {

public void paint() { : }}

package graphics;

public class Circle {

public void paint() { : }}

package graphics;

public class RoundRectangle {

public void paint() { : }}

package graphics;

public class RoundRectangle {

public void paint() { : }}

graphics\Circle.java graphics\rectangle\RoundRectangle.java

Page 21: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

21

http://atit.patumvan.com

Using Class of a Different Package

: graphic.Circle c = new graphics.Circle(); c.paint(); :

: graphic.Circle c = new graphics.Circle(); c.paint(); :

import graphics.Circle; : Circle c = new Circle(); c.paint(); :

import graphics.Circle; : Circle c = new Circle(); c.paint(); :

import graphics.*; : Circle c = new Circle(); c.paint(); :

import graphics.*; : Circle c = new Circle(); c.paint(); :

Import all classes of package

Import class

● Automatically imported package● java.lang● DefaultPackage● Current Package

● Packge name folder structure→

● CLASSPATH: list of folder where java looks for package

Page 22: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

22

http://atit.patumvan.com

Access ControlPackage Same

Class

Package

Package Other

Subclass

Any

subclass

public

protected

default

private

Class

Yes

Yes

Yes

Yes

Package

Yes

Yes

Yes

No

Subclass

Yes

Yes

No

No

Any

Yes

No

No

No

Page 23: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

23

http://atit.patumvan.com

Static Members

● Similar to global

● Class member (static) vs. Instance member (default)

● Static member belong to the class, not to the objects

● Static member can be access from class or from object

public class BankAccount {

private static long number = 0; : public BankAccount(Customer cus) { number++; holder = cus; cus.newAccount(this); } : public static long getNumber() { return number; } :}

public class BankAccount {

private static long number = 0; : public BankAccount(Customer cus) { number++; holder = cus; cus.newAccount(this); } : public static long getNumber() { return number; } :}

BankAccount account = new BankAccount(customer);long accountNumber = account.getNumber(); :long lastAccountNumber = BankAccount.getNumber();

BankAccount account = new BankAccount(customer);long accountNumber = account.getNumber(); :long lastAccountNumber = BankAccount.getNumber();

Page 24: OOP: Classes and Objects

Object-Oriented Programming: Classes and Objects in Java

24

http://atit.patumvan.com

final Variables

● Similar to constant, there value cannot be changed

● Initialization is mandatory

● More efficient: static final

● A constructor cannot be final

public class Circle { static final double PI = 3.141592653589793; :}

public class Circle { static final double PI = 3.141592653589793; :}