"Java Objects and Classes": Presented By: Rodgen C. Gamalo BSIT-3 Student

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

“Java Objects and Classes”

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.

Object-oriented programming has several advantages over procedural programming:

 OOP is faster and easier to execute


 OOP provides a clear structure for the programs
 OOP helps to keep the Java code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code and shorter
development time

Java Classes/Objects

Java is an object-oriented programming language.

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.

A Class is like an object constructor, or a "blueprint" for creating objects.

Create a Class

To create a class, use the keyword class:

MyClass.java

Create a class named "MyClass" with a variable x:

public class MyClass {


int x = 5;
}

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

Create an object called "myObj" and print the value of x:

public class MyClass {


int x = 5;

public static void main(String[] args) {


MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}

Multiple Objects

You can create multiple objects of one class:

Example

Create two objects of MyClass:

public class MyClass {


int x = 5;

public static void main(String[] args) {


MyClass myObj1 = new MyClass(); // Object 1
MyClass myObj2 = new MyClass(); // Object 2
System.out.println(myObj1.x);
System.out.println(myObj2.x);
}
}

Using Multiple Classes

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);
}
}

Java Class Attributes

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

Create a class called "MyClass" with two attributes: x and y:

public class MyClass {


int x = 5;
int y = 3;
}

Another term for class attributes is fields.

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

Create an object called "myObj" and print the value of x:

public class MyClass {


int x = 5;

public static void main(String[] args) {


MyClass myObj = new MyClass();
System.out.println(myObj.x);
}
}

Modify Attributes

You can also modify attribute values:

Example

Set the value of x to 40:

public class MyClass {


int x;

public static void main(String[] args) {


MyClass myObj = new MyClass();
myObj.x = 40;
System.out.println(myObj.x);
}
}

Or override existing values:

Example

Change the value of x to 25:

public class MyClass {


int x = 10;

public static void main(String[] args) {


MyClass myObj = new MyClass();
myObj.x = 25; // x is now 25
System.out.println(myObj.x);
}
}

If you don't want the ability to override existing values, declare the attribute as final:

Example

public class MyClass {


final int x = 10;

public static void main(String[] args) {


MyClass myObj = new MyClass();
myObj.x = 25; // will generate an error: cannot assign a value to a final variable
System.out.println(myObj.x);
}
}

The final keyword is useful when you want a variable to always store the same value, like PI
(3.14159...).

The final keyword is called a "modifier".

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

Change the value of x to 25 in myObj2, and leave x in myObj1 unchanged:

public class MyClass {


int x = 5;

public static void main(String[] args) {


MyClass myObj1 = new MyClass(); // Object 1
MyClass myObj2 = new MyClass(); // Object 2
myObj2.x = 25;
System.out.println(myObj1.x); // Outputs 5
System.out.println(myObj2.x); // Outputs 25
}
}

Multiple Attributes

You can specify as many attributes as you want:

Example

public class Person {


String fname = "John";
String lname = "Doe";
int age = 24;

public static void main(String[] args) {


Person myObj = new Person();
System.out.println("Name: " + myObj.fname + " " + myObj.lname);
System.out.println("Age: " + myObj.age);
}
}

Java Class Methods

You learned from the Java Methods chapter that methods are declared within a class, and that
they are used to perform certain actions:

Example

Create a method named myMethod() in MyClass:

public class MyClass {


static void myMethod() {
System.out.println("Hello World!");
}
}
myMethod() prints a text (the action), when it is called. To call a method, write the method's
name followed by two parentheses () and a semicolon;

Example

Inside main, call myMethod():

public class MyClass {


static void myMethod() {
System.out.println("Hello World!");
}

public static void main(String[] args) {


myMethod();
}
}

// Outputs "Hello World!"

Static vs. Non-Static

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 class MyClass {


// Static method
static void myStaticMethod() {
System.out.println("Static methods can be called without creating objects");
}

// 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

MyClass myObj = new MyClass(); // Create an object of MyClass


myObj.myPublicMethod(); // Call the public method on the object
}
}

Note: You will learn more about these keywords (called modifiers) in the Java Modifiers
chapter.

Access Methods With an Object

Example

Create a Car object named myCar. Call the fullThrottle() and speed() methods on the myCar
object, and run the program:

// Create a Car class


public class Car {

// Create a fullThrottle() method


public void fullThrottle() {
System.out.println("The car is going as fast as it can!");
}

// Create a speed() method and add a parameter


public void speed(int maxSpeed) {
System.out.println("Max speed is: " + maxSpeed);
}

// Inside main, call the methods on the myCar object


public static void main(String[] args) {
Car myCar = new Car(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}

// The car is going as fast as it can!


// Max speed is: 200

Example explained

1) We created a custom Car class with the class keyword.

2) We created the fullThrottle() and speed() methods in the Car class.

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 (;).

A class must have a matching filename (Car and Car.java).

Using Multiple Classes

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!");
}

public void speed(int maxSpeed) {


System.out.println("Max speed is: " + maxSpeed);
}
}
OtherClass.java
class OtherClass {
public static void main(String[] args) {
Car myCar = new Car(); // Create a myCar object
myCar.fullThrottle(); // Call the fullThrottle() method
myCar.speed(200); // Call the speed() method
}
}

You might also like