00-PF211-Mod 3

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Classes and Objects

Outline:
• Classes and Objects
• Attributes
• Methods
• Constructors
• Code Sample
Class
• A class can be defined as a template/blue print
that describes the behaviors/states that object of
its type support.
• A type or classification to which all similar
objects belong.
• An important point to be noted here is that a
class is just a DESIGN where everything is
defined or we can say it is a type.
Example:
Human Beings
are broadly* categorized into

Male Female
Example:
In every Human, we have

Body Parts: hands, legs, eyes, heart etc.

Most body parts are same for both Male and Female. While
some body parts are different.

Body Functions: Walk, Eat, See, Talk, Hear etc.


Object
• Object – is an instance of a class which has physical
existence.
• Objects have states and behaviors.
• Suppose there a HumanBeing named

“Joshua” “Michelle”

• “Joshua” and “Michelle” is an object of HumanBeing class.


• Note: Object has physical existence while a class is just a logical
definition
• Book – a class
• Object Oriented Programming by Skiena
• Reading in the Philippine History by _
• Dog – a class
• askal
• k9
• poodle
• Food
• Adobo
• Kare-kare
• Artist
• Makeup Artist
• Tattoo Artist
Short Exercise:
Identify if it is a class or object
• Person
• Michael
• Animal
• Dog
• Bird
• Student
• Customer
• Course
• Dove
• BS Information System
• Book
Classes in Java
• A class is a blue print from which individual objects are
created. A sample of a class is given below:

Attributes

Methods
• Person
• name, age, address
• sleep, walk, eat
Attributes
• A class can contain any of the following variable types.
• Local variables: Variables defined inside methods,
constructors or blocks are called local variables. The
variable will be declared and initialized within the method
and the variable will be destroyed when the method has
completed.
• Instance variables: Instance variables are variables within
a class but outside any method. These variables are
instantiated when the class is loaded. Instance variables can
be accessed from inside any method, constructor or blocks
of that particular class.
• Class variables: Class variables are variables declared
within a class, outside any method, with the static keyword.
Instance variables
Class variable

Local variable
Constructor
• When discussing about classes, one of the most
important subtopic would be constructors. Every
class has a constructor. If we do not explicitly
write a constructor for a class the Java compiler
builds a default constructor for that class.
• Each time a new object is created, at least one
constructor will be invoked. The main rule of
constructors is that they should have the same
name as the class. A class can have more than
one constructor.
Constructor
Creating an Object
• As mentioned previously, a class provides the
blueprints for objects. So basically an object is created
from a class. In Java the new keyword is used to
create new objects.
• There are three steps when creating an object from a
class:
• Declaration: A variable declaration with a variable name
with an object type.
• Instantiation: The 'new' keyword is used to create the
object.
• Initialization: The 'new' keyword is followed by a call to a
constructor. This call initializes the new object.
Accessing Instance Variables
and Methods
• Instance variables and methods are accessed via
created objects. To access an instance variable
the fully qualified path should be as follows:
Note:
• When developing applications in Java, hundreds
of classes and interfaces will be written,
therefore categorizing these classes is a must as
well as makes life much easier.
Coding Example:
• Create a class named Student.
• The source filename should be Student.java.
• The Student class has four (4) instance variables
age, name, address and course.
• This class should have one explicitly defined
constructor, which takes a parameter for id.
• In this class create a method to display the Student
details.
• In your main method create a five (5) objects using
the constructor.

You might also like