0% found this document useful (0 votes)
46 views39 pages

Lecture1 1class

The document discusses object-oriented programming concepts like classes, objects, UML notation, constructors, and accessibility. It explains that classes define objects of the same type and act as templates, while objects are instances of classes with their own identity, state, and behaviors. The document also covers constructor and method declaration and invocation, accessing object data fields and methods, copying references versus primitives, and default values.

Uploaded by

afif ilhamsyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views39 pages

Lecture1 1class

The document discusses object-oriented programming concepts like classes, objects, UML notation, constructors, and accessibility. It explains that classes define objects of the same type and act as templates, while objects are instances of classes with their own identity, state, and behaviors. The document also covers constructor and method declaration and invocation, accessing object data fields and methods, copying references versus primitives, and default values.

Uploaded by

afif ilhamsyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

OBJECT-ORIENTED

PROGRAMMING

DR NOOR AZAH SAMSUDIN


[email protected]
FACULTY OF COMPUTER SCIENCE AND
INFORMATION TECHNOLOGY, UNIVERSITI
TUN HUSSEIN ONN MALAYSIA (UTHM)
Objectives
• Lecture 1.1
• Class
• Constructors
• Data fields
• UML notation
CLASS
Object
• object
• An entity in the real world that can be
distinctly identified.
• A student, a desk, a circle, a button, and
even a loan can all be viewed as objects.
• has a unique identity, state, and
behaviors.
• State = a set of data fields (also known
as properties) with their current values.
• Behavior = a set of methods
Class vs.
Object
• Classes are constructs that define
objects of the same type

Class Name: Circle A class template

Data Fields:
radius is _______

Methods:
getArea

Circle Object 1 Circle Object 2 Circle Object 3 Three objects of


the Circle class
Data Fields: Data Fields: Data Fields:
radius is 10 radius is 25 radius is 125
Class
Example
class Circle {
/** The radius of this circle */
double radius = 1.0; Data field

/** Construct a circle object */


Circle() {
}
Constructors
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}

/** Return the area of this circle */


double getArea() { Method
return radius * radius * 3.14159;
}
}
UML
UML Notation
• UML class diagram

UML Class Diagram Circle Class name

radius: double Data fields

Circle() Constructors and


Circle(newRadius: double) methods
getArea(): double

circle2: Circle circle3: Circle UML notation


circle1: Circle
for objects
radius = 1.0 radius = 25 radius = 125
CONSTRUCTOR
Constructor
• Constructors are a special kind of
methods that are invoked to construct
objects.
Circle() Constructor
{ with no
} argument

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

• Referencing the object’s data fields:


myCircle.radius

• Invoking the object’s method:


myCircle.getArea()
Data Fields

• The data fields can be of reference


types
• Example: name data field is of type
String
public class Student {
String name; // name has default value null
int age; // age has default value 0
boolean isScienceMajor; // has default value false
char gender; // c has default value '\u0000‘

//methods …
}
null

• If a data field of a reference type does


not reference any object, the data field
holds a special literal value: null.
Default Values

• The default value of a data field is


• null for a reference type
• 0 for a numeric type
• false for a boolean type
• '\u0000' for a char type
• However, Java assigns no default value
to a local variable inside a method.
Default Values

• Java assigns no default value to a local


variable inside a method.

public class Test {


public static void main(String[] args) {
int x; // x has no default value
String y; // y has no default value
System.out.println("x is " + x);
System.out.println("y is " + y);
}
}

Compilation error:
variables not initialized
Copying

• Primitive type copy

Primitive type assignment i = j

Before: After:

i 1 i 2

j 2 j 2
Copying

• Reference type copy

Object type assignment c1 = c2

Before: After:

c1 c1

c2 c2

c1: Circle c2: Circle c1: Circle c2: Circle


radius = 5 radius = 9 radius = 5 radius = 9
Garbage
Collector
• As shown in the previous figure, after
the assignment statement c1 = c2, c1
points to the same object referenced by
c2.
• The object previously referenced by c1
is no longer referenced.
• This object is known as garbage.
• Garbage is automatically collected by
JVM.
Garbage
Collector
• TIP: If you know that an object is no
longer needed, you can explicitly
assign null to a reference variable for
the object.
• The JVM will automatically collect the
space if the object is not referenced by
any variable.
Accessibility

1. Package access (default in Java)


• The class, variable, or method can be
accessed by any class in the same package.

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

• Access C1 methods & fields from C2/C3?


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();
}
private int z; }
}
}
public void m1()
{..}
void m2() {..}
private void m3()
{..}
}
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

• The private modifier restricts access to within a


class
• The default modifier restricts access to within a
package
• The public modifier enables unrestricted
access.
Accessibility

• Example:
public class Foo {

private boolean x;

public void test()


{
System.out.println(x);
System.out.println(convert()); OK
}

private int convert(boolean b)


{
return x ? 1 : -1;
}
}
Accessibility

• 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.

• To make class easy to maintain.


get/set

• The get and set methods are used to


read and modify private properties.
• Data encapsulation

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

• Data encapsulation – demo example


Program

• Passing by value for primitive type value


(the value is passed to the parameter)
• Passing by value for reference type
value (the value is the reference to the
object)
ARRAY OF OBJECTS
Array of Objects

• An array of objects is actually an array of


reference variables.
• So invoking circleArray[1].getArea()
involves two levels of referencing.
• circleArray references to the entire array.
• circleArray[1] references to a Circle
object.

Circle[] circleArray = new Circle[10];


Array of Objects

Circle[] circleArray = new Circle[10];

circleArray reference circleArray[0] Circle object 0


circleArray[1]

… Circle object 1

circleArray[9] Circle object 9

You might also like