CA591/598 – Chapter 8 Object-Oriented Programming - DCU

handprintSoftware and s/w Development

Nov 18, 2013 (3 years and 7 months ago)

71 views

CA591/598


Chapter 8

Object
-
Oriented Programming

Dr. James Murphy

jamurphy@computing.dcu.ie

Classes and Objects


Understand what is meant by object
-
oriented.


Create objects from existing class.


Call methods of an object.


Define a class from which objects can be
created.


Understand visibility modifiers.


Concept of encapsulation.

Object
-
Oriented Programming


Aims at modelling real
-
world
objects
, e.g. an
employee, bank account, or student.



Software components built around objects:


Represent
attributes

of object.


e.g. student ID, account number, etc.


represented by data fields


Operations

that relate to that object.


Represented by methods.

Classes


Objects are
defined

by classes.


Model/blueprint from which an object is created.


Defines type of data that will be held in an object.


Defines code for the methods.


Everything an object can do is defined by its
methods.


After a class has been defined, can
create

an
object from it:


Called
instantiation



individual objects are called
instances

of a class.


A class is an abstraction, whereas an object is a
real entity.

Examples of Classes

BankAccount

accountNo

name

balance

lodgeMoney()

withDraw()

getAccount()

getName()

getBalance()

Car

colour

engine

max speed

start()

accelerate()

brake()

Class Name

Attributes (data)

Operations

(methods)

Attributes and Methods


Set of attributes (data members of class):


Data represented by variables.


Set of methods which can access or change the
attributes (method members of class).


Methods operate on the variables of the class.



Variables are the elements that differentiate one object
of the class from another (e.g. studentID).


Every object has its own set of variables and has
access to the public methods of that class.


Called
instance

variables.

Creating Objects


First look at example of
using

a class (class
BankAccount).



To use a class, need to know names of methods and
what they do, but not details.


Need to know what arguments expected by methods and
what is returned.


Also need to know how to construct an object
(instantiate).



In BankAccount case study, will create two bank
accounts and lodge and withdraw money.

Description of Class BankAccount


Fields (attributes of a bank account):


accountNo (integer)


name (String)


balance (double)



Constructor (what attributes can be set up
when object constructed):


public BankAccount(int accNo, accName)


Account number and name should be supplied.
Balance is set to 0.

Description of Class BankAccount


Methods:


public void lodgeMoney(double amt)


Adds amount specified to the balance.


public boolean withDraw(double amt)


Subtracts amount specified from balance, if balance
sufficient. If balance insufficient, returns false.


public int getAccount()


Returns the account number


public String getName()


Returns account name.


public double getBalance()


Returns balance in the account.

Declaring and Creating Objects


Declaring an object:


ClassName ObjectName;


e.g.
BankAccount joe;


Creates variable called joe, containing reference
(address) of a BankAccount object.


Create (construct) an object:


joe = new BankAccount(1234, “Joe Soap”);


Declare and create object in single statement:


BankAccount joe = new BankAccount(1234, “Joe Soap”);

UseAccounts

accountNo

name

balance

accountNo

name

balance

1000

3000

joe

mary

UseAccounts

1234

Joe Soap

200.0

accountNo

name

balance

1235

Mary White

500.0

accountNo

name

balance

1000

3000

joe

mary

Defining Classes
-

BankAccount


Two levels of definition of classes:


Abstract level:


Defines outside view of class without concern for
inner workings (“black box”)


Implementation level:


Contains detailed code which describes the
attributes (variables) and operations (methods).

Class Declaration Format

class Classname

{

//
data members

(describes the attributes)

private datatype variable1;

private datatype variable2;



etc.


//
constructor

public ClassName(parameter list)

{

}


//
method members

public returnType method1Name(parameter list)

{

}


…any other methods


} // end class

Constructors


When creating an object of a class, special
kind of method called
constructor

invoked.


If no constructor defined, default constructor
applied (variables set to zero).


Means to initialise variable values.


Two special characteristics of constructor:


Never returns a value (must not specify return type,
not even void)


Always has same name as the class (note
uppercase first letter)

Overloading Constructors


In Chapter 6, looked at methods with same
name but different
signatures

(e.g. different
parameter types).


Called
overloading
.


Can overload constructors, to be able to
construct objects with different kinds of initial
values.


For example, one constructor sets variables to
default values, while another one supplies input
values.

Case Study 2: Rectangle Class


Instance variables:


Length


Width


Constructors:


Constructor with defualt values length 10, width 12.


Constructor with length and width supplied as input arguments


Methods required:


Method to calculate and return perimeter.


Method to calculate and return area of rectangle.


Write a program to test the class:


Create 2 rectangles: one the default size (10*12), and the
other 8*6.

Review of Structure of Programs

1.
The early programs (up to lab test 1):


Just one class containing a main method into which we put
everything.


Suited short programs, learning syntax.


class ClassName

{


public static void main(String[] args)


{




// define variables and constants




// instructions referring to variables.


}

}

// end class

Review of Structure of Programs

2.
Introduction to Methods:


Used methods to delegate tasks.


Static methods (could be addressed without instantiating an
object)


Main program called a method and passed values
(arguments) to it.


Method header defined a formal parameter list.


Local variables could be defined in method.



static double calcCharge(char cType, int days)

{



// local variables



// code to do the method’s jobs



// return statement

}

Review of Structure of Programs

3.
Classes for use in Object
-
Oriented Program:


Software components which contain data and
methods


“Blueprints” for objects.


Instance variables represent attributes


Methods refer to operations related to object.


Writing methods are similar to before except:


Can directly refer to instance variables in this class.


When writing a class, think in general terms, i.e.
don’t have to think about any particular person’s
account when creating BankAccount class.