Classes & Objects: By: Maria Cristina B. Nazareno
Classes & Objects: By: Maria Cristina B. Nazareno
Classes & Objects: By: Maria Cristina B. Nazareno
2/12/16
INTRODUCTION
Java is an Object-Oriented Language. As a language that has the
Object Oriented feature, Java supports the following fundamental
concepts:
Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
created by: ma. cristina b. nazareno
Instance
2/12/16
If you can touch it, name it, or talk about it, it is likely
to be an object.
Objects can be physical things or conceptual.
Humans seem to want to think in terms of objects and
their relationships with each other.
2/12/16
WHAT IS A CLASS?
A class can be defined as a template/blue
print that describes the behaviors/states that
object of its type support.
2/12/16
CLASS DEFINITION
A Java class definition contains
Fields - what properties an object has
The values assigned to the fields define the state of
the object. (e.g., the car is painted silver, has a half
a tank of gas, and is stopped.)
2/12/16
CLASS DEFINITION
You define a class by using the class
keyword along with the class name, like
this:
class MyClass
{
}
created by: ma. cristina b. nazareno
2/12/16
CLASS DEFINITION
To create an object from a class, you type the class's
name followed by the name of the object. For
example, the line below creates an object from the
MyClass class:
Name of an
object
Automatically calls
the constructor
Dynamically create
object using new
2/12/16
ASSIGNMENT
2/12/16
2/12/16
11
2/12/16
12
age = newAge;
}
}
2/12/16
13
2/12/16
14
Benefits of Encapsulation
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.
The users of a class do not know how the class stores its data. A
class can change the data type of a field and users of the class
do not need to change any of their code.
2/12/16