Objects(extension of variables) a class template Data Fields radius is Methods getAe Circle Object I Circle Object 2 I Circle Object 3 ← Three objects of the circle Data fields Data fields Data fields radius is 10 radius is radIus Is An object has both a state and behavior State defines the object Behavior defines what the object does What is the size of an object in the memory
11 Objects (extension of variables) • An object has both a state and behavior. • State defines the object, • Behavior defines what the object does. • What is the size of an object in the memory? Class Name: Circle Data Fields: radius is _______ Methods: getArea Circle Object 1 Data Fields: radius is 10 Circle Object 2 Data Fields: radius is 25 Circle Object 3 Data Fields: radius is 125 A class template Three objects of the Circle class
Class Class defines the type of an object the kinds of operations that it performs a Java class uses variables to define data fields and methods to define behaviors a class provides a special type of methods known as constructors which are invoked to construct objects from the class
12 Class • Class defines • the type of an object • the kinds of operations that it performs. • A Java class uses variables to define data fields and methods to define behaviors. • A class provides a special type of methods, • known as constructors, which are invoked to construct objects from the class
Classes class circle /* The radius of this circle * double radius =1.07 Data field 大大 Construct a ciro1 e object* Circle i Constructors 大大 Construct a circle object Circle(double newRadius)[ radius newRadiusi /大大 Return the area of this circle+/ double getArea()I Method return radius radius * 3.14159
13 Classes class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * 3.14159; } } Data field Method Constructors
Class diagram UML Class diagram Circle Class name radius. double t Data field Circle Constructors and Circle(newRadius: double) Methods getArea(: double circle. Circle circle: Circle circle3cie← IML notation for objects radius: 10 radius: 25 adis: 125 14
14 Class Diagram Circle radius: double Circle() Circle(newRadius: double) getArea(): double circle1: Circle radius: 10 Class name Data fields Constructors and Methods circle2: Circle radius: 25 circle3: Circle radius: 125 UML Class Diagram UML notation for objects
Person State/attributes-variables Operation/action - method name age gender Figure 2.3: Class specification of Person 0 2003 Brooks/Cole Publishing/thol Learning 15
15 © 2003 Brooks/Cole Publishing / Thomson Learning™ Figure 2.3: Class specification of Person State/attributes—variables Operation/action – methods