2.Ders
2.Ders
► Software components
Objects
Class Name: Circle A class template
Data Fields:
radius is _______
Methods:
getArea
► An object has both a state and behavior. The state defines the object,
and the behavior defines what the object does.
3
Java Classes
► A class is a collection of fields (data) and methods (procedure or
function) that operate on that data.
Attributes
center
radius
Operations circumference()
area()
Defining a Java Class
► Syntax:
class ClassName{
[fields declaration]
[methods declaration]
}
class Circle {
public double x, y; // center coordinates
public double r; // radius of the circle
}
circleA circleB
null null
Points to nothing (Null Reference) Points to nothing (Null Reference)
Creating Objects of a Class
► Objects are created by using the new keyword
Circle circleA;
circleA = new Circle();
circleA circleB
Two different
circle objects!
Creating Objects of a Class
circleA = new Circle();
circleB = new Circle() ;
circleB = circleA;
circleA.x = 25.0;
circleA.y = 25.0;
circleA.r = 3.0;
yourCircle.radius = 100;
14
Trace Code, cont.
yourCircle.radius = 100;
: Circle
radius: 5.0
Create a circle
15
Trace Code, cont.
yourCircle.radius = 100;
: Circle
Assign object reference
to myCircle radius: 5.0
16
Trace Code, cont.
myCircle reference value
yourCircle no value
Declare yourCircle
17
Trace Code, cont.
myCircle reference value
yourCircle no value
: Circle
radius: 0.0
18
Trace Code, cont.
myCircle reference value
radius: 0.0
19
Trace Code, cont.
myCircle reference value
: Circle
Change radius in
yourCircle radius: 100.0
20
Circle Class Altogether
public class Circle {
public double x, y; // center of the circle
public double r; // radius of the circle
► Helpful to keep all class files used by a program in the same directory
Reference Data Fields
The data fields can be of reference types. For example, the following
Student class contains a data field name of the String type.
23
The null Value
If a data field of a reference type does not reference any
object, the data field holds a special literal value, null.
24
Default Value for a Data Field
The default value of a data field is null for a reference type, 0 for a numeric type,
false for a boolean type, and '\u0000' for a char type. However, Java assigns no
default value to a local variable inside a method.
25
Local Variable Example
Java assigns no default value to a local variable inside a method.
Compilation error:
variables not initialized
26
public class Dog {
public String name; // Instance variables
public String breed;
public int age;
► A public method that changes the data stored in one or more private
instance variables is called a mutator method, a set method, or a
setter.
► The names of mutator methods typically begin with set.
Circle Class with Getters/Setters
public class Circle {
public double x, y; // center of the circle
public double r; // radius of the circle
► A class can have more than one constructor as long as they have
different signatures (i.e., different input arguments syntax).
Circle Class with Constructor
public class Circle {
public double x, y; // center of the circle
public double r; // radius of the circle
// Constructor
public Circle(double centerX, double centerY, double radius) {
x = centerX;
y = centerY;
r = radius;
}
// Constructor
public Circle(double centerX, double centerY, double radius) {
x = centerX;
y = centerY;
r = radius;
}
public Circle() {
x = 0; y = 0; r = 1.0;
}
► Java creates only one copy for a static variable which can be used
even if the class is never instantiated.
Using Static Variables
► Define the variable by using the static keyword
public class Circle {
// Class variable, one for the Circle class.
// To keep number of objects created.
public static int numCircles = 0;
// Constructor
Circle (double x, double y, double r){
this.x = x;
this.y = y;
this.r = r;
numCircles++;
}
} Circle circleA = new Circle(10, 12, 20);
// numCircles = 1
Circle circleB = new Circle(5, 3, 10);
// numCircles = 2
Instance vs. Static Variables
► Instance variables: One copy per object. Every object has its own
instance variables.
► e.g. x,y,r (center and radius of the circle)
Math.pow(2.0, 3.0) // 8
Math.max(5, 6) // 6
Math.round(6.2) // 6
Math.sqrt(4.0) // 2.0
Object Cleanup (Destructor)
► Recall: Memory deallocation is automatic in Java
► No dangling pointers and no memory leak problem.
► Java allows to define finalize method, which is invoked (if defined)
just before the object destruction.
► This presents an opportunity to perform record maintenance
parameterTester(c1, c2);
► Information hiding:
► Designing a method so it can be used without knowing details
► private means that part of the class is hidden and inaccessible by code
outside of the class
► part of the implementation
► data fields are generally private
The public and private Modifiers
► Type specified as public
► Any other class can directly access that object by name
► Examples:
private int id;
private String name;
} box.width = 6;
is illegal since width is private
► Keeps remaining elements of the class consistent
Point Class
// A Point object represents an (x, y) location.
public class Point {
private int x;
private int y;
public Point(int initialX, int initialY) {
x = initialX;
y = initialY;
}
public double distanceFromOrigin() {
return Math.sqrt(x * x + y * y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setLocation(int newX, int newY) {
x = newX;
y = newY;
}
public void translate(int dx, int dy) {
x = x + dx;
y = y + dy;
}
}
Client code
public class PointMain4 {
public static void main(String[] args) {
// create two Point objects
Point p1 = new Point(5, 2);
Point p2 = new Point(4, 3);
► Class implementation
► Contains private variables
► Includes definitions of public and private methods
Encapsulation
► A well encapsulated class definition
Programmer who
uses the class
Encapsulation – Best Practices
► Preface class definition with comment on how to use class
► Place a comment before each public method heading that fully specifies
how to use method.
► When implementing a method of a class, use the class’s set and get
methods to access the class’s private data. This simplifies code
maintenance and reduces the likelihood of errors.
► If they are not initialized in their declarations, they must be initialized in all
constructors
Plus signs
imply public
access
UML Class Diagrams
► Contains more than interface, less than full
implementation
► Each class
► Placed in a separate file
► Has this line at the beginning of the file
package Package_Name;
► Name of package uses relative path name starting from any directory
in class path
Package Names and Directories
► A package name
Time Class Case Study: Creating Packages
► Single-type-import declaration
► Imports a single class
► Type-import-on-demand declaration
► Imports all classes in a package
public static char getAverage (char first, char second) { average1= 45.0
return (char) (((int) first + (int) second) / 2); } average2= 2.0
} average3 = b
Overloading and Type Conversion
► Overloading and automatic type conversion can conflict