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.
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
UML and Decomposition Class Diagram –Represents relationships between classes Composition relationship –One class composed of others as member variables Class Support class
Clock Example ClockProject package Main.java visual application Stores current hour and minute Allows hour and minute to be set Increments to next second or minute Clock.java Clock.java business logic clock
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)
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: 12 minute: 15 hour: 3 minute: 42 c1c2
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!
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
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
Methods Often return information about state of object in form helpful to user –Example: toString returns state in HH:MM forma t 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; }
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
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
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++)
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
Using a Clock Object Business logic objects used by other classes
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(); }
Using a Clock Object
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)
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 Wheel 4 Spoke String char *
Aggregation Example Goal: Display list of times in each time zone Solution: Array of 4 clock objects
Aggregation Example Construct array of Clocks Use loop to construct each array element –Use different initial hour for each
Aggregation Example Use loop to call nextMinute and toString for each Clock in array –Print tabs between each clock time, newline at end