2. Learning Objectives
• To introduce to classes and
objects in Java.
• To understand how some of the
Object-Oriented (OO) concepts
learnt so far are supported in
Java.
• To understand important features
in Java classes and objects.
2
3. Introduction
• Java is a true OO language and
therefore the underlying
structure of all Java programs
is classes.
• Anything we wish to represent
in Java must be encapsulated
in a class that defines the
“state” and “behavior” of the
basic program components
known as objects. 3
4. Definitions
Class :
• A class is an entity that determines how an object
will behave and what the object will contain. In
other words, it is a blueprint or a set of instruction
to build a specific type of object. It provides initial
values for member variables and member
functions or methods.
Object :
• An object is nothing but a self-contained
component that consists of methods and properties
to make a data useful. It helps you to determines
the behavior of the class.
6. Illustration 1 of Classes and Objects
• As we start this class, I want to use this
simple illustration 1 to explain the difference
between class and objects.
• Take the example of animals to represent
class. The objects in this case, would be the
different animals that make up the class.
• These different animals (objects) operate
individually and collaboratively (in an
ecosystem) to make produce outcomes that
can be collectively classified as animal
characteristics.
8. Illustration 2 of Classes and Objects
• In this second Illustration 2, think of class as a car,
and the object as the (i) methods and (ii) properties
that make that class (car) useful.
• So in the case of objects as methods, this would
include aspects like refueling and speed setting
• For objects as properties as this would include
things like maximum speed of the car or fuel
capacity
• Later on in this lesson we will see how objects
manifest as methods and as properties as we build
class examples
10. Illustration 3 of Classes and Objects
• Remember that the objects forming classes have
attributes even though they look different.
• Also remember that each object has a different
behavior.
• As seen in illustration 3, the class of cat has
different cats (objects) that have different
attributes such as color (some are pink, black
etc.), and different behavior (some are playful,
other calm, others playful etc.).
12. Illustration 4 of Classes and Objects
• Based on illustration 4, that is how one would
represent a class
• You can see that objects (different attributes +
methods) have been used to develop the class.
• An example of an attribute bodycolor would
therefore, be defined as:
• And an example of a method forward () would
therefore, be defined as:
14. Defining A Class
• The basic syntax for a class definition is as follows:
• However, a bare bone class with no objects would
look something like this:
14
public class Circle {
// my circle class
}
class ClassName [extends
SuperClassName]
{
[fields declaration]
[methods declaration]
}
15. Adding Fields: Class Circle with fields
• Below is an illustration of adding objects to a
class like a circle. Remember that a circle has
different attributes. In this case the attributes we
are adding/defining include: centre coordinate
and radius of the circle
• Point to Note*The fields (data) are also called
the instance variables.
15
public class Circle {
public double x, y; // centre coordinate
public double r; // radius of the circle
}
16. Adding Methods
• Apart from adding fields, one also has to add
methods to a class.
• A class with only data fields has no life or cannot
respond to any messages/instructions.
• Methods are declared inside the body (objects) of
the class and immediately after the declaration of
data fields.
• The general form of a method declaration is:
16
type MethodName (parameter-list)
{
Method-body;
}
17. Practical Example of Adding Methods to Class Circle
17
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
18. Data Abstraction
• Assigning Attributes to Objects helps with
the process known as data abstraction
• Data abstraction is the process of hiding
certain details and showing only essential
information to the user.
• Abstraction can be achieved with either
abstract classes or interfaces
19. Illustration 5: What Happens if an Object is Not Defined for A Class
19
aCircle = new Circle();
bCircle = new Circle() ;
bCircle = aCircle;
P
aCircle
Q
bCircle
Before Assignment
P
aCircle
Q
bCircle
After Assignment
20. Illustration 5: What Happens if an Object is Not Defined for A Class
• If the object does not have a
reference and cannot be used in future.
• The object becomes a candidate for
automatic garbage collection.
• Java automatically collects garbage
periodically and releases the memory used
to be used in the future.
20
Q
21. Accessing Object/Circle Data
• During the development of classes and the
definition of objects, it is important to access the
defined.
• In the case of Java, this is similar to C syntax as
demonstrated below for class circle:
21
Circle aCircle = new Circle();
aCircle.x = 2.0 // initialize center and radius
aCircle.y = 2.0
aCircle.r = 1.0
ObjectName.VariableName
ObjectName.MethodName(parameter-list)
22. Executing Methods in Object/Circle
• After defining data in objects, its now time
to execute (use methods), as illustrated
below:
22
Circle aCircle = new Circle();
double area;
aCircle.r = 1.0;
area = aCircle.area();
sent ‘message’ to aCircle
23. Using Class after Using/Executing Objects: 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);
}
}
23
[raj@mundroo]%: java MyMain
Radius=5.0 Area=78.5
Radius=5.0 Circumference =31.400000000000002
24. Summary
• Basically, we have learned that:
• Classes, objects, and methods are the
basic components used in Java
programming.
• 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
24
26. Extra Resources & Information
• Here is a list of resources that you can examine
after class to understand the topic of classes and
objects better: (Remember that I will post this
presentation in the class announcement section in
Blackboard)
• 1) https://www.youtube.com/watch?v=W-
D71ZeMixQ&list=PLBlnK6fEyqRiwWLbSXKFtdGV
8OVqr9dZr
• 2) https://snakebear.science/11-
Classes/classes.html