Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SH1801

Classes and Objects (Part 1)

I. Object-Oriented Programming
• A programming style in Java and other programming langauges intends to make thinking about
programming closer to the real world.
• In OOP, each object is an independent unit with a unique identity, just as objects in the real
world are. This allows program objects to properly represent their real-world counterparts.
• Objects also have characteristics, which are used to describe them. For example, a car can be
red or blue, a mug can be full or empty, and so on. These characteristics are also called
attributes.
• An attribute describes the current state of an object. In the real world, each object behaves in
its own way; a car can move, a phone can ring or vibrate, and so on. The same applies to
program objects, as behavior is specific to the object’s type.
A. Classes
• A class describes what an object derived from it will be, but is a completely separate entity
from the object itself. In other words, classes can be described as blueprints, descriptions,
or definitions for an object.
• You can use the same class as a blueprint for creating multiple objects. This can be initially
achieved by defining the class to be used, which then becomes a blueprint for object
creation.
• Each class has a name, and each is used to define attributes and behavior. Some examples
of attributes and behavior:
Attributes Behavior
name walk
height run
weight jump
gender speak
age sleep
II. Methods
A. Methods
• Methods define behavior. A method is a collection of statements that are grouped together to
perform an operation. An example of a method is the System.out.println() method.
• You can define your own methods to perform your desired tasks.
• For example:
class MyClass {

static void sayHello() {


System.out.println("Hello World!");
}

public static void main(String[ ] args) {


sayHello();
}
}
// Outputs "Hello World!"

06 Handout 1 *Property of STI


 student.feedback@sti.edu Page 1 of 5
SH1801

o The example code declares a method called “sayHello”, which prints a text and then gets
called in main.

B. Calling Methods
• Calling a method can be done as many times as necessary.
• When a method runs, the code jumps down to where the method is defined, executes the
code inside of it, then goes back and proceeds to the next line.
• For example:
class MyClass {

public static void main(String[ ] args) {


sayHello();
sayHello();
sayHello();
}
}

/* Hello World!
Hello World!
Hello World! */

C. Method Parameters
• You can also create a method that takes some data, called parameters, along with it
when you call it.
• Parameters can be written within the method’s parentheses (( )).
• For example, we can modify our sayHello() method to take and output a String
parameter:
class MyClass {

static void sayHello(String name) {


System.out.println("Hello " + name);
}

public static void main(String[ ] args) {


sayHello("David");
sayHello("Amy");
}

}
/* Hello David
Hello Amy */

• The method in the example takes a String called name as a parameter, which is used in
the method’s body. Then, when calling the method, we pass the parameter’s value inside
the parentheses.
• Methods can take multiple parameters, each separated by a comma (,).

III. Methods Return Types


A. The Return Type
• The return keyword can be used in methods to return a value.

06 Handout 1 *Property of STI


 student.feedback@sti.edu Page 2 of 5
SH1801

• For example:
static int sum(int val1, int val2) {
return val1 + val2;
}
• The static keyword can be used to allow declaration of static variables and methods that
belong to the class instead of a specific instance (such as the main method):
class MyClass {

static int sum(int val1, int val2) {


return val1 + val2;
}

public static void main(String[ ] args) {


int x = sum(2, 5);
System.out.println(x);
}
}
// Outputs "7"
• As the method returns a value, the value can then be assigned to a variable.
// returns an int value 5
static int returnFive() {
return 5;
}

// has a parameter
static void sayHelloTo(String name) {
System.out.println("Hello " + name);
}

// simply prints"Hello World!"


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

IV. Class Attributes


A. Defining Attributes
• A class has attributes and methods. The attributes are basically variables within a class.
• For example, a class called Vehicle with its corresponding attributes and methods:
public class Vehicle {
int maxSpeed;
int wheels;
String color;
double fuelCapacity;

void honk() {
System.out.println("Beep!");
}
}
• The maxSpeed, wheels, color, and fuelCapacity are the attributes of our Vehicle class, and honk()
is the only method.

06 Handout 1 *Property of STI


 student.feedback@sti.edu Page 3 of 5
SH1801

B. Creating Objects
• We can create multiple objects of our Vehicle class, and use the dot syntax to access their
attributes and methods:
class MyClass {
public static void main(String[ ] args) {
Vehicle v1 = new Vehicle();
Vehicle v2 = new Vehicle();
v1.color = "red";
v2.honk();
}
}

V. Access Modifiers
• public is an access modifier, meaning that it is used to set the level of access:
public static void main(String[ ] args)
• You can use access modifiers for classes, attributes, and methods.
• For classes, the available modifiers are public or default (left blank), as described below:
o public: The class is accessible by any other class.
o default: The class is accessible only by classes in the same package – a group of
similar types of classes.
• The following choices are available for attributes and methods:
o default - a variable or method declared with no access control modifier is available
to any other class in the same package
o public - accessible from any other class.
o protected - provides the same access as the default access modifier, with the addition
that subclasses can access protected methods and variables of the superclass.
o private - accessible only within the declared class itself.
• For example:
public class Vehicle {
private int maxSpeed;
private int wheels;
private String color;
private double fuelCapacity;

public void honk() {


System.out.println("Beep!");
}
}

VI. Getters and Setters


• Used to effectively protect data, particularly when creating classes.
• For each variable, the get method returns its value, while the set method sets the value.
• Getters start with get, followed by the variable name, with the first letter of the variable
name capitalized.
• Setters start with set, followed by the variable name, with the first letter of the variable
name capitalized.
• For example:

06 Handout 1 *Property of STI


 student.feedback@sti.edu Page 4 of 5
SH1801

public class Vehicle {


private String color;

// Getter
public String getColor() {
return color;
}

// Setter
public void setColor(String c) {
this.color = c;
}
}
• The getter method returns the value of the attribute.
• The setter method takes a parameter and assigns it to the attribute.
• Once the getter and setter have been defined, they can be used in the main method:
public static void main(String[ ] args) {
Vehicle v1 = new Vehicle();
v1.setColor("Red");
System.out.println(v1.getColor());
}

//Outputs "Red"

References:
Deitel, H., & Deitel, P. (2014). Java: How to program-early objects (10th ed.). Prentice Hall.
SoloLearn.com – Java Programming. Retrieved on March 07, 2018 from
https://www.sololearn.com/Play/Java#

06 Handout 1 *Property of STI


 student.feedback@sti.edu Page 5 of 5

You might also like