Lecture1 1class
Lecture1 1class
PROGRAMMING
Data Fields:
radius is _______
Methods:
getArea
Circle(double newRadius)
Constructor
{
with
radius = newRadius; argument
}
Constructor
• Constructors must have the same name
as the class itself.
• Constructors do not have a return type—
not even void.
• Constructors are invoked using the new
operator when an object is created.
• Constructors play the role of initializing
objects.
Constructor
• Creating objects using constructors
new ClassName();
new Circle();
new Circle(5.0);
Constructor
• A class may be declared without
constructors.
• In this case, a no-arg constructor with an
empty body is implicitly declared in the
class.
• This constructor, called a default
constructor, is provided automatically
only if no constructors are explicitly
declared in the class.
Creating
Objects
• Declaring & creating objects in a single
step
Circle myCircle = new Circle();
declare create
• Two steps
//step 1: declare
Circle myCircle;
//step 2: create
myCircle = new Circle();
ACCESSING DATA
FIELDS & METHODS
Access
//methods …
}
null
Compilation error:
variables not initialized
Copying
Before: After:
i 1 i 2
j 2 j 2
Copying
Before: After:
c1 c1
c2 c2
2. public
• The class, data, or method is visible to any
class in any package.
3. private
• The data or methods can be accessed only
by the declaring class.
Accessibility
Example
Package 1 Package 2
public class C2 {
public class C1 { public class C3 {
void aMethod() {
public int x; void aMethod() {
C1 o = new C1();
int y; C1 o = new C1();
//can access o.x;
private int z; //can access o.x;
//can access o.y;
//cannot access
//cannot access
public void m1() //o.y;
//o.z;
{..} //cannot access
//can invoke
void m2() {..} //o.z;
//o.m1();
private void m3() //can invoke
//can invoke o.m2()
{..} //o.m1();
//cannot invoke
} //cannot invoke
//o.m3();
//o.m2();
}
//cannot invoke
}
//o.m3();
}
}
Accessibility
Example
• Which classes can access C1?
Package 1 Package 2
public class C2 {
class C1 { public class C3 {
//can access C1?
… //can access C1?
}
} //can access C2?
}
Accessibility
Example
• Which classes can access C1?
Package 1 Package 2
public class C2 {
class C1 { public class C3 {
//can access C1
… //cannot access C1
}
} //can access C2
}
Accessibility
Summary
• Example:
public class Foo {
private boolean x;
• Example:
public class Test {
public static void main(String[]
args)
{
Foo foo = new Foo();
Error
System.out.println(foo.x);
!
System.out.println(foo.convert(foo.x));
}
}
• Because x and convert are private members
Why Private?
• To protect data.
Circle
-radius: double The radius of this circle (default: 1.0).
-numberOfObjects: int The number of circle objects created.
The - sign
indicates +Circle() Constructs a default circle object.
private +Circle(radius: double) Constructs a circle object with the specified radius.
modifier +getRadius(): double Returns the radius of this circle.
+setRadius(radius: double): void Sets a new radius for this circle.
+getNumberOfObject(): int Returns the number of circle objects created.
+getArea(): double Returns the area of this circle.
Program
… Circle object 1