Lecture 6 - Classes and Objects

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

CS175: Programming in Java

Lecture 6

Dr. Juma Lungo


[email protected]
Classes and Objects in Java

Basics of Classes in Java

2
Contents

l Introduce to classes and objects in Java.

l Understand how some of the OO concepts


learnt so far are supported in Java.

l Understand important features in Java


classes.

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

l A class is a collection of fields (data) and


methods (procedure or function) that operate
on that data.

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

public class Circle {


// my circle class
}

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:

type MethodName (parameter-list)


{
Method-body;
}

9
Adding Methods to Class Circle

public class Circle {

public double x, y; // centre of the circle


public double r; // radius of circle

//Methods to return circumference and area


public double circumference() {
return 2*3.14*r;
}
public double area() {
return 3.14 * r * r; Method Body
}
}
10
Data Abstraction

l Declare the Circle class, have created a new


data type – Data Abstraction

l Can define variables (objects) of that type:

Circle aCircle;
Circle bCircle;

11
Class of Circle cont.

l aCircle, bCircle simply refers to a Circle


object, not an object itself.

aCircle bCircle

null null

Points to nothing (Null Reference) Points to nothing (Null Reference)


12
Creating objects of a class

l Objects are created dynamically using the


new keyword.
l aCircle and bCircle refer to Circle objects
aCircle = new Circle() ; bCircle = new Circle() ;

13
Creating objects of a class

aCircle = new Circle();


bCircle = new Circle() ;

bCircle = aCircle;

14
Creating objects of a class
aCircle = new Circle();
bCircle = new Circle() ;

bCircle = aCircle;

Before Assignment After Assignment

aCircle bCircle aCircle bCircle

P Q P Q

15
Automatic garbage collection
Q
l The object does not have a
reference and cannot be used in future.

l The object becomes a candidate for


automatic garbage collection.

l Java automatically collects garbage


periodically and releases the memory used
to be used in the future.
16
Accessing Object/Circle Data
l Similar to C syntax for accessing data
defined in a structure.

ObjectName.VariableName
ObjectName.MethodName(parameter-list)

Circle aCircle = new Circle();

aCircle.x = 2.0 // initialize center and radius


aCircle.y = 2.0
aCircle.r = 1.0
17
Executing Methods in Object/Circle

l Using Object Methods:


sent ‘message’ to aCircle

Circle aCircle = new Circle();

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

l Classes, objects, and methods are the basic


components used in Java programming.
l We have discussed:
• How to define a class
• How to create objects
• How to add data fields and methods to classes
• How to access data fields and methods to classes

20
Objects and Classes

A little more on Objects and Classes in Java


Outline
l OO Programming Concepts
l Creating Objects and Object Reference Variables
• Differences between primitive data type and object type
• Automatic garbage collection
l Constructors
l Modifiers (public, private and static)
l Instance and Class Variables and Methods
l Scope of Variables
l Use the this Keyword
l Case Studies (Mortgage class and Count class)
OO Programming Concepts

An object A Circle object

data field 1 Data Field


radius = 5

... State
Method
data field n findArea

method 1

... Behavior

method n
Class and Objects

Circle UML Graphical notation for classes

radius: double UML Graphical notation for fields

UML Graphical notation for methods


findArea(): double

new Circle() new Circle()

circle1: Circle circlen: Circle UML Graphical notation


for objects
radius = 2 ... radius = 5
Class Declaration
class Circle {
double radius = 1.0;

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();

The object reference is assigned to the object


reference variable.
Declaring/Creating Objects
in a Single Step

ClassName objectReference = new ClassName();

Example:
Circle myCircle = new Circle();
Differences between variables of
primitive Data types and object
types
Primitive type int i = 1 i 1

Object type Circle c c reference

c: Circle
Created using
new Circle() radius = 1
Copying Variables of Primitive Data
Types and Object Types

Primitive type assignment Object type assignment


i=j c1 = c2

Before: After: Before: After:

i 1 i 2 c1 c1

j 2 j 2 c2 c2

c1: Circle c2: Circle

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

l Invoking the object’s method:


objectReference.method
myCircle.findArea()
Example 1: Using Objects

l Objective: Demonstrate creating


objects, accessing data, and using
methods.
Constructors
Circle(double r) {
radius = r;
} Constructors are a
special kind of
Circle() { methods that are
radius = 1.0; invoked to construct
} objects.

myCircle = new Circle(5.0);

You might also like