23
Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Embed Size (px)

Citation preview

Page 1: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Java Class Syntax

CSIS 3701: Advanced Object Oriented Programming

Page 2: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Class Decomposition

• Often decompose complex class into simpler support classes

• Why?– Easier to write/debug/maintain group of simple

classes than one complex class– Simpler classes can be refactored into general

purpose tools usable by others• Refactoring: redesigning classes after an implementation

stage to improve modularity, efficiency, etc.

Page 3: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

UML

• Universal Modeling Language (UML)– Excellent tool for analysis and design

• Features:– Graphical in nature

• How we tend to do design

– Simple enough for customer to understand– Well-defined enough to allow developers to create

system– Common design specification language

• Experienced developer should be able to immediately understand system from diagram

Page 4: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

UML and Decomposition

• Class Diagram– Represents relationships between classes

• Composition relationship– One class composed of others as member variables

ClassSupport

class

Page 5: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Clock Example

ClockProject package

Main.javavisual application

• Stores current hour and minute• Allows hour and minute to be set• Increments to next second or minute

Clock.javabusiness logic

clock

Page 6: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Basic Class Structure

package ClockProject;

public class Clock {

}

Package declaration

Class name must be same as name of file

Makes class available for use by other classes

Only one class per file

All code for class in file (no separate headers and code)

Page 7: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Member Variables

public class Clock {

private int hour;

private int minute;

}

Internal representation may not be directly accessed by other objects – must use methods instead

• Represent current state of

the object

• Exist for lifetime of object

• “Shared” by all methods and constructors

• Each object maintains own copy, possibly with different values

hour: 12minute: 15

hour: 3minute: 42

c1 c2

Page 8: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Constructors

• Called when object created with new

Clock c = new Clock();

– Sets initial state of member variables

– Must have same name as class

public class Clock {

…public Clock() {

hour = 0;

minute = 0;}

hour and minute initially both 0

Note that variables not redeclared!

Page 9: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Methods

• Generally manipulate member variables– May also have local variables like a function

public class Clock {

public void nextMinute() {

minute++; if (minute > 59) { minute = 0; hour++; if (hour > 23) {

hour = 0; } }

}

Note that methods and constructors usually public so may be called by other objects

Page 10: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Methods

• Often provide direct access to state variables

public void setMinute(int m) {

minute = m;

}

public void setHour(int h) {

hour = h;

}public int getMinute() {

return minute;

}public int getHour() {

return hour;

}

“Setter” methods

“Getter” methods

Note that h, m are local variables

Page 11: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Methods

• Often return information about state of object in form helpful to user– Example: toString returns state in HH:MM format

public String toString() {

String result = "";

if (hour < 10) result += "0" + hour;

else result += hour;

result += ":";

if (minute < 10) result += "0" + minute;

else result += minute;

return result;

}

Page 12: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Overloading

• Giving multiple definitions to same name• Most often done with constructors

– Required to have same name as class– May need to construct objects in different ways

• Legal if compiler can disambiguate based on parameters– Number of parameters– Type of parameters

Page 13: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Overloading

• Example: Overloaded constructor to set initial time

public class Clock {

…public Clock() {

hour = 0;

minute = 0;}

public Clock(int h, int m) {

hour = h;

minute = m;}

0-parameter “default” constructor

2-parameter “overloaded” constructor

Page 14: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Overloading

• Easy for compiler to disambiguate:

Clock c1 = new Clock();

Must be default constructor

Clock c2 = new Clock(12, 47);

Must be overloaded constructor

Note: Must use () in constructor call even if no parameters (unlike C++)

Page 15: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

The “this” object

• Reference from object to itself– Stores address of object– Implicit state variable in all objects

• Internal use of state variables implicitly use “this” public void setHour(int h) {

this.hour = h;

}public int getHour() {

return this.hour;

}

37A4

this 37A4

Manipulate the hour member variable of “this” object

Page 16: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Using a Clock Object

• Business logic objects used by other classes

Page 17: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Using a Clock Object

• Other objects composed of support objects– Contain object as member variable– Construct that object (often when it is constructed)– Call its methods as needed

public class Main … {…private Clock clock; …public Main() {

clock = new Clock();}

Page 18: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Using a Clock Object

Page 19: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Aggregation

• Often keep track of many objects simultaneously

• Simplest idea: Array of objects– Must construct array and all objects in array– Use loop to call method for all objects in array

• Basic syntax:for (int i = 0; i < A.length; i++) { A[i].method(params)

Page 20: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

UML for Aggregation

• Give number of entities aggregate type contains– Can be range (1..4)– Can be unlimited

* any number from 0 to ∞+ any number from 1 to ∞

Car Wheel4

Spoke5..20

String char*

Page 21: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Aggregation Example

• Goal: Display list of times in each time zone

• Solution: Array of 4 clock objects

Page 22: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Aggregation Example

• Construct array of Clocks • Use loop to construct each array element

– Use different initial hour for each

Page 23: Java Class Syntax CSIS 3701: Advanced Object Oriented Programming

Aggregation Example

• Use loop to call nextMinute and toString for each Clock in array– Print tabs between each clock time, newline at end