Lecture 6 - Classes and Objects
Lecture 6 - Classes and Objects
Lecture 6 - Classes and Objects
Lecture 6
2
Contents
3
Introduction
l Java is a true OO language and therefore the underlying
structure of all Java programs is classes.
l Anything we wish to represent in Java must be
encapsulated in a class that defines the “state” and
“behaviour” of the basic program components known as
objects.
l Classes create objects and objects use methods to
communicate between them. They provide a convenient
method for packaging a group of logically related data
items and functions that work on them.
l A class essentially serves as a template for an object and
behaves like a basic data type “int”. It is therefore
important to understand how the fields and methods are
defined in a class and how they are used to build a Java
program that incorporates the basic OO concepts such as
encapsulation, inheritance, and polymorphism.
4
Classes
Circle
centre
radius
circumference()
area()
5
Classes
l A class is a collection of fields (data) and methods
(procedure or function) that operate on that data.
l The basic syntax for a class definition:
class ClassName [extends
SuperClassName]
{
[fields declaration]
[methods declaration]
}
6
Classes
l Bare bone class – no fields, no methods
7
Adding Fields: Class Circle with fields
l Add fields
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
l The fields (data) are also called the instance
variables.
8
Adding Methods
l A class with only data fields has no life. Objects created
by such a class cannot respond to any messages.
l Methods are declared inside the body of the class but
immediately after the declaration of data fields.
l The general form of a method declaration is:
9
Adding Methods to Class Circle
Circle aCircle;
Circle bCircle;
11
Class of Circle cont.
aCircle bCircle
null null
13
Creating objects of a class
bCircle = aCircle;
14
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P Q P Q
15
Automatic garbage collection
Q
l The object does not have a
reference and cannot be used in future.
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
double area;
aCircle.r = 1.0;
area = aCircle.area();
18
Using Circle Class
// Circle.java: Contains both Circle class and its user class
//Add Circle class code here
class MyMain
{
public static void main(String args[])
{
Circle aCircle; // creating reference
aCircle = new Circle(); // creating object
aCircle.x = 10; // assigning value to data field
aCircle.y = 20;
aCircle.r = 5;
double area = aCircle.area(); // invoking method
double circumf = aCircle.circumference();
System.out.println("Radius="+aCircle.r+" Area="+area);
System.out.println("Radius="+aCircle.r+" Circumference ="+circumf);
}
}
[raj@mundroo]%: java MyMain
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
19
Summary
20
Objects and Classes
... State
Method
data field n findArea
method 1
... Behavior
method n
Class and Objects
double findArea(){
return radius * radius * 3.14159;
}
}
Declaring Object Reference Variables
ClassName objectReference;
Example:
Circle myCircle;
Creating Objects
objectReference = new ClassName();
Example:
myCircle = new Circle();
Example:
Circle myCircle = new Circle();
Differences between variables of
primitive Data types and object
types
Primitive type int i = 1 i 1
c: Circle
Created using
new Circle() radius = 1
Copying Variables of Primitive Data
Types and Object Types
i 1 i 2 c1 c1
j 2 j 2 c2 c2
radius = 5 radius = 9
Garbage Collection
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 useful. This
object is known as garbage.
Garbage is automatically
collected by JVM.
Garbage Collection, cont
TIP: If you know that an object
is no longer needed, you can
explicitly assign null to a
reference variable for the
object. The Java VM will
automatically collect the space
if the object is not referenced
by any variable.
Accessing Objects
l Referencing the object’s data:
objectReference.data
myCircle.radius