Académique Documents
Professionnel Documents
Culture Documents
Specific Objectives:
At the end of the topic session, the students are expected to:
Cognitive:
Affective:
Psychomotor:
MATERIALS/EQUIPMENT:
o topic slides
o OHP
TOPIC PREPARATION:
TOPIC PRESENTATION:
The topic will introduce the students on how to write classes, declare
member variables, methods, and constructors.
This will be the suggested flow of discussion for the course topic:
Let us consider again the object Student. There are many possible
attributes that a Student can have like Name, ID number, Birthday,
Address, and GPA. All of these are data that pertain to a single student.
Another example would be the object Course. This object can have
attributes like Course Number, Course Name, Prerequisites, and
Credits. These attributes can characterize a Course object.
NOTE: Provide examples for both physical and conceptual objects then
let the students enumerate its possible attributes. You may write their
Now let us consider the methods or behaviors that an object has. Think
of methods as activities or abilities that a certain object can perform. For
example, a Student may enroll in a course or may drop a certain course.
These are some examples of activities that the Student object may
perform.
The Course object also has its own methods. The Course object is
responsible if a student can register or determine if the student has met
the prerequisite prior to taking another subject.
NOTE: Provide another object example on class and let the students
define its own methods. You may use the Car and Department or
maybe animals (e.g., dog or bird) as examples.
Another way of reading “is an instance of” might be “is an example of”.
We can then say “Juan dela Cruz is an example of a Person.
[Note that this relationship can also be read as “Juan dela Cruz IS A
person.” This, however, is not used since the “is-a” relationship is used
in object-oriented programming to show the relationship between a
superclass and its subclass/es.]
//core here…
}
The private here means that the variables are only accessible within
the class. Other objects cannot access these variables directly.
You can also declare a variable static by using the static keyword.
For example,
Page 5 of 16
Objects and Classes
Object-Oriented Programming
Private – specifies that the class members are only accessible by the
class they are defined in. For example,
Default – specifies that only classes in the same package can have
access to the class’ variables and methods. There is no actual keyword
for the default modifier. It is applied in the absence of an access
modifier. For example,
run
runFast
getBackground
getAverage
compareTo
setX
isEmpty
A method can be declared void that doesn’t return a value. If you try
to return a value from a method that is declared void, a compiler error
will be generated.
Any method that is not declared void must contain a return statement
with a corresponding return value:
return returnValue;
Take note that the data type of the return value must match the
method’s declared return type. For example, you cannot return an
integer value from a method declared to return a Boolean.
get<NameOfField>
In the given example, public modifier means that the method can be
called from objects outside the class. The return type of the method is
declared String, which means that the method should return a value of
type String. The name of the method is getName.
set<NameOfField>
For example,
The method is declared public which means that it can be called from
objects outside the class. void means that the method does not return
any value. setName is the name of the method. String temp is the
parameter that will be used inside the method.
As mentioned earlier, void means that the method does not return any
value. Typically, mutators do not return any value although some
programmers prefer to return the new value of the field for checking
purposes (i.e., method returns a specified value when an error/exception
occurs).
The modifier is declared public which means that the method can be
called from objects outside the class. The method is static and
should be called by typing, [ClassName].[methodName]. For example,
in this case, we call the method StudentRec.getStudCount(). The
return type of the method is int, which means that the method should
return an integer value. getStudCount is the name of the method.
Here is an example:
class Data
{
public int add(int x) {
return x + 5;
Notice that there are several methods having the same name add().
Even though the names of the methods are the same, the methods can
be distinguished by the number of parameters each method takes.
Constructors Constructors
Pages 27-32 of 40
A constructor is a special type of method that establishes or creates a
new object. In Java, constructors have the same name as their class
and have no return value in their declaration.
For example,
For example,
public StudentRecord()
{
//insert code here…
}
Person pedro;
pedro = new Person( );
In this example, the first line just declares a variable pedro that is of
type Person. The second line uses the new operator to invoke the class
constructor Person()to create the object and assign this to the variable
pedro. Every class must have at least one constructor to instantiate it.
If you don’t declare any constructors in your class, the compiler will
automatically declare one with no arguments.
class Person {
String name;
//Constructor
public Person() {
name = “pedro”;
}
int getName(){
return name;
}
}
class SamplePerson {
public static void main(String[] args)
{
//Instantiation
Person x;
x = new Person();
System.out.print(“\nHello ” +
x.getName());
}
}
public StudentRec()
{
//code here…
}
public StudentRec(String Temp)
{
this.name = temp;
}
public StudentRec(String name, String address)
{
this.name = name;
this.address = address;
}
public StudentRec(double mGrade, double sGrade,
double eGrade)
{
mathGrade = mGrade;
scienceGrade = sGrade;
englishGrade = eGrade;
}
int x = blank.x;
The expression blank.x means "go to the object blank refers to, and
get the value of x." In this case we assign that value to a local variable
named x. The purpose of the dot notation is to identify which variable
you are referring to unambiguously.
this.<nameOfTheField>
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
The this keyword indicates that the value of the method parameters x
and y will be assigned to the fields x and y, respectively. Without the
this keyword, the use of variables x and y as method parameters
preclude access to the fields x and y.
Packages Packages
Pages 37-38 of 40
A package is a named collection of classes. It is Java’s means of
grouping related classes and interfaces together in a single unit.
(Interfaces will be discussed further in the succeeding sessions)
import <nameOfPackage>;
For example,
import java.awt.*;
import java.io;
Exercises Exercises
Pages 39-40 of 40
Ask the students to perform the following exercises and then discuss the
answers in the class.
What are the class variables? What are the instance variables?
EVALUATION/GENERALIZATION:
o A method may or may not return a value. One that does not
return a value is called a void method.
o A constructor is a special method that is executed when a new
object is created. Its purpose is to initialize the object into a
valid state.
o A public method that retrieves a property of an object is called
an accessor.
o A public method that changes a property of an object is called a
mutator.
o Methods with the same name are called overloaded methods.
o Ask the students to perform the exercises provided in the slide
and the laboratory exercise.
REFERENCES:
Page 16 of 16
Objects and Classes