OOP is something that must be learned to understand Java more deeply. Because Java itself is a language designed for OOP.
The proof:
class Hello {
public static void main(String[] args){
System.out.println("Hello World!");
}
}
OOP is known as a modern programming technique that is more efficient and widely used in the Framework .
If you want to understand the Framework, first study the OOP concept
So there is no reason not to study OOP.
What will we learn in this article?
- What is OOP?
- Objects and Classes
- Class Diagram
Let's start…
What is OOP?
OOP ( Object Oriented Programming ) or in Indonesian known as object oriented programming (PBO) is a paradigm or programming technique that is object oriented.
In OOP, functions and variables are wrapped in an object or class that can interact with each other, thus forming a program.
Variables in objects will store data from objects. While the function will determine its operation.
Examples of objects in the real world: Cars, Birds, Drones, Tables, Trees, etc.
------------------OBJEK
Drone
------------------Variabel/Atribut
energi = 100;
ketinggian = 200;
kecepatan = 29;
------------------Fungsi
terbang();
matikanMesin();
turun();
maju();
mundur();
belok();
------------------
All objects in the real world that have properties and behavior, we can represent in code.
Keywords to remember:
"The object contains data and functions"
Class and Object
Class is a design or blue print of an object.
While the object in programming is a variable that is an instance of Class .
Instance can be interpreted as a form of class .
Class contains definitions of variables and functions that describe an object.
In OOP:
- Variables are called attributes or properties ;
- The function is called the method .
Example Class :
class NamaClass {
String atribut1;
String atribut2;
void namaMethod(){ ... }
void namaMethodLain(){ ... }
}
Then, we usually create objects (instances) like this:
NamaClass namaObj = new NamaClass();
Keywords
new
function to create new objects from certain classes .
After creating the object, we can access the attributes and methods of the object.
Example:
namaObj.namaMethod();
namaObj.atribut1;
The dot (
.
) functions to access attributes and methods.Examples of OOP Programs
We will use Netbeans, please create a new project.
Fill in the name of the project with "BelajarOOP" and don't check "Create Main Class" .
After that, please create a new package . Right-click on <default package> , then select New-> Java Package .
Fill in the package name with "base" :
After that, in the package
dasar
, please create a new classnamed Player.java
.
Right-click on the package
dasar
and then choose New-> Java Class . After that, fill in the name with Player.java
.
Please write the code
Player.java
like this:package dasar;
public class Player {
// definisi atribut
String name;
int speed;
int healthPoin;
// definisi method run
void run(){
System.out.println(name +" is running...");
System.out.println("Speed: "+ speed);
}
// definisi method isDead untuk mengecek nilai kesehatan (healthPoin)
boolean isDead(){
if(healthPoin <= 0) return true;
return false;
}
}
Next, create a new class named
Game.java
and fill in the code like this:package dasar;
public class Game {
public static void main(String[] args){
// membuat objek player
Player petani = new Player();
// mengisi atribut player
petani.name = "Petani Kode";
petani.speed = 78;
petani.healthPoin = 100;
// menjalankan method
petani.run();
if(petani.isDead()){
System.out.println("Game Over!");
}
}
}
Try execution
Game.java
by right-clicking Game.java
then selecting Run File . Then the result:
Try changing the value
healthPoin
to 0
.petani.healthPoin = 0;
After that, try executing again:
The player
petani
is dead, the game is finished!How come? Explain Don!
Well, we start from making classes .
First of all, we create a class with a name
Player
. This class defines the Player object in the Game with the following attributes:name
is the name of the object;speed
is speed;healthPoin
is the health value of the player, usually abbreviatedhp
.
Then the class
Player
has a method:run()
to move the player;isDead()
to check the health condition of the player. This method will return a valuetrue
if the valuehp
is smaller or equal to zero (0
), otherwise it will return a valuefalse
.
Next we create a new object from the class
Player
in the Game
named class petani
.// membuat objek player
Player petani = new Player();
After that, fill in the attributes. Because if it's not filled it will be null and can cause NullPointerException .
Next we try modifying the value
hp
to zero.
As a result, a message
Game Over!
will be displayed. Because the method isDead()
returns a value true
if the value is hp
less or equal to zero.Examples of OOP 2 Programs: Drone Programs
For the next practice, try making a class
Drone
.class Drone {
// atribut
int energi;
int ketinggian;
int kecepatan;
String merek;
// method
void terbang(){
energi--;
if(energi > 10){
// terbang berarti nilai ketinggian bertambah
ketinggian++;
System.out.println("Dorne terbang...");
} else {
System.out.println("Energi lemah: Drone nggak bisa terbang");
}
}
void matikanMesin(){
if(ketinggian > 0){
System.out.println("Mesin tidak bisa dimatikan karena sedang terbang");
} else {
System.out.println("Mesin dimatikan...");
}
}
void turun(){
// ketinggian berkurang, karena turun
ketinggian--;
energi--;
System.out.println("Drone turun");
}
void belok(){
energi--;
System.out.println("Drone belok");
// belok ke mana? perlu dicek :)
}
void maju(){
energi--;
System.out.println("Drone maju ke depan");
kecepatan++;
}
void mundur(){
energi--;
System.out.println("Drone mundur");
kecepatan++;
}
}
How to determine the attributes and methods of an object?
As explained earlier. The object has data (attributes) and operations (functions).
The key:
- The attribute is like the characteristics of an object.
- The method is like behavior or operation.
Example:
Suppose we want to make a class from
Senjata
.
What are the characteristics of weapons or data that can be used as attributes?
- number of bullets
- type
- weapon name
- weight
- color
- etc
Then what is the behavior or operation (function) that can be done?
- shoot ()
- throw ()
- less Bullet ()
- reload ()
- etc.
Then the class can be made like this:
class Senjata {
// atribut
String nama;
int jumlahPeluru;
// method
void tembak(){
//..
// setelah nembak, kurangi peluru
jumlahPeluru--;
}
void reload(){
//..
// kalau di-reload maka jumlah peluru diisi ulang
jumlahPeluru = 300;
}
}
It's easy not ...
Get to know the Class Diagram
Class Diagram is a diagram that describes the relationships between classes . We can create Class Diagrams with design applications (CASE), such as StarUML.
A class is represented by a table of 1 column and 3 rows.
The first line contains the class name ; The second line contains attributes; and the third line contains the method.
In addition, there is a line that describes the relationship between classes .
Class Diagrams are usually used by software engineers to design software with the OOP paradigm.
As programmers, we only need to know how to read and write it in code.
If you are interested in class diagrams, you can read books about UML.
0 Komentar untuk "Learning Java OOP: Understanding the Basic Concepts of OOP (for Beginners)"
Silahkan berkomentar sesuai artikel