"Java Objects and Classes": Presented By: Rodgen C. Gamalo BSIT-3 Student
"Java Objects and Classes": Presented By: Rodgen C. Gamalo BSIT-3 Student
"Java Objects and Classes": Presented By: Rodgen C. Gamalo BSIT-3 Student
Presented By:
Rodgen C. Gamalo
BSIT-3 Student
Presented To:
Jifford R. Romasanta
Instructor
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures or methods that perform operations on the
data, while object-oriented programming is about creating objects that contain both data and
methods.
Java Classes/Objects
Everything in Java is associated with classes and objects, along with its attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as weight and color, and
methods, such as drive and brake.
Create a Class
MyClass.java
Remember that a class should always start with an uppercase first letter, and that the name of the
java file should match the class name.
Create an Object
In Java, an object is created from a class. We have already created the class named MyClass, so
now we can use this to create objects.
To create an object of MyClass, specify the class name, followed by the object name, and use the
keyword new:
Example
Multiple Objects
Example
You can also create an object of a class and access it in another class. This is often used for
better organization of classes (one class has all the attributes and methods, while the other class
holds the main() method (code to be executed)).
Remember that the name of the java file should match the class name. In this example, we have
created two files in the same directory/folder:
MyClass.java
OtherClass.java
MyClass.java
public class MyClass {
int x = 5;
}
OtherClass.java
class OtherClass {
public static void main(String[] args) {
MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}
In the previous chapter, we used the term "variable" for x in the example (as shown below). It is
actually an attribute of the class. Or you could say that class attributes are variables within a
class:
Example
Accessing Attributes
You can access attributes by creating an object of the class, and by using the dot syntax (.):
The following example will create an object of the MyClass class, with the name myObj. We use
the x attribute on the object to print its value:
Example
Modify Attributes
Example
Example
If you don't want the ability to override existing values, declare the attribute as final:
Example
The final keyword is useful when you want a variable to always store the same value, like PI
(3.14159...).
Multiple Objects
If you create multiple objects of one class, you can change the attribute values in one object,
without affecting the attribute values in the other:
Example
Multiple Attributes
Example
You learned from the Java Methods chapter that methods are declared within a class, and that
they are used to perform certain actions:
Example
Example
You will often see Java programs that have either static or public attributes and methods.
In the example above, we created a static method, which means that it can be accessed without
creating an object of the class, unlike public, which can only be accessed by objects:
Example
An example to demonstrate the differences between static and public methods:
// Public method
public void myPublicMethod() {
System.out.println("Public methods must be called by creating objects");
}
// Main method
public static void main(String[] args) {
myStaticMethod(); // Call the static method
// myPublicMethod(); This would compile an error
Note: You will learn more about these keywords (called modifiers) in the Java Modifiers
chapter.
Example
Create a Car object named myCar. Call the fullThrottle() and speed() methods on the myCar
object, and run the program:
Example explained
3) The fullThrottle() method and the speed() method will print out some text, when they are
called.
4) The speed() method accepts an int parameter called maxSpeed - we will use this in 8).
5) In order to use the Car class and its methods, we need to create an object of the Car Class.
6) Then, go to the main() method, which you know by now is a built-in Java method that runs
your program (any code inside main is executed).
7) By using the new keyword we created a Car object with the name myCar.
8) Then, we call the fullThrottle() and speed() methods on the myCar object, and run the
program using the name of the object (myCar), followed by a dot (.), followed by the name of
the method (fullThrottle(); and speed(200);). Notice that we add an int parameter of 200 inside
the speed() method.
Remember that..
The dot (.) is used to access the object's attributes and methods.
To call a method in Java, write the method name followed by a set of parentheses (), followed by
a semicolon (;).
Like we specified in the Classes chapter, it is a good practice to create an object of a class and
access it in another class.
Remember that the name of the java file should match the class name. In this example, we have
created two files in the same directory:
Car.java
OtherClass.java
Car.java
public class Car {
public void fullThrottle() {
System.out.println("The car is going as fast as it can!");
}