Pemrograman Berorientasi Objek : Mengenal Kelas, Objek, Atribut, dan Method

Pemrograman Berorientasi Objek : Mengenal Kelas, Objek, Atribut, dan Method



Class in Java Programming Language



Java Programming Language

Java API (Application Programming Interface) provides almost 4000 classes that we can use
  • Cross platform is one of the main advantages of Java PL (achieved using Java Virtual Machine)
  • Philosophy : "Write once, run anywhere"



The Implementation Phase, using Java Programming Language and Java Virtual Machine: 



Let us see a demo example: Simple Calculator 



Assume that you’ve done all the steps : 
  • Solve the problem / build your algorithm
  • Write the source code: Write the class using Java PL and store it on a text file (.java)
  • Compile the source code using JDK, we will get the Java bytecode (.class)
  • We run the bytecode in Java Virtual Machine ( JVM will interpret the bytecode )


Class



It’s time to code the object into a something that will be understood by a computer (a software object). But, there is a problem: We may not want to describe each individual object because there might be too many objects. 


Idea : Several objects may have many commonalities. Why don’t we describe what is common just once ?

We can group the objects that we have identified based on their properties & capabilities. For example, all books have the same properties & capabilities although they may have different values for their properties. A book shelf have different properties & capabilities compared to the books.




In OOP, a cookie cutter is something we called a Class. Class is a blueprint for objects with similar properties & capabilities. Class can only define what properties & capabilities an object will have. A class cannot store any values. Only object can store any values. Therefore, we only need to define a single class to describe the properties & capabilities of similar objects.

Class also defines a template for making an actual object. A class also defines the initial values for the objects’ properties.


Structures


The structure of a class in Java 



ClassName can be changed into any name we want. The Java code to complete the creation of a class must be inserted between the curly brackets ({ and }) 

Identifier is a name assigned for classes, methods or variable.
  • First character must be a letter or underscore, but the rest is free (including number and dollar sign)
  • Spaces or symbols (? / %) cannot be used
  • Several words cannot be used because Java use it as reserved words. Example: new, class, extends, etc

Rules of naming for classes: 
  • Class name should be a noun(s)
  • The first letter and also the first letter of each internal word should be capitalized
  • Avoid acronyms and/or abbreviations that leads to confusion. Bottom line: when somebody read it, he/she must instantly understand what the class is.
  • Please do not be lazy to put several character !!
  • Example: Ball, TabletPC, DoubleBed


It is important to include several information into the source code :






Software Objects



After we define the class, we can create the Software Objects. Software Object is an instance of the class: 
  • Made from a class template 
  • We can have many objects as an instances of the same class 
  • All of them will have the same properties, but might have different values 
  • Different instances might have different values for their attributes/instance variable 
  • The process of creating an object is called instantiating an object 

We write a computer program by modeling problem as a set of collaborating component/object:
  • Similar to building with Lego, you should determine what the building blocks are.
  • Some of the components are pre-defined but some of them should be designed by yourself.

Later on, you should put them together so they can cooperate properly


Attributes & Methods



In Object Oriented Programming, we create a class and
  • Implement properties as instance variable a.k.a. Attributes
  • Implement capabilities as methods

Each software object must communicate with others to accomplish tasks. They send messages (or instruction) to one another to invoke others capabilities or change properties. When a human object send a message to a mailbox object (e.g. To know how many item inside it), we say a human object calls a method on a mailbox object.




Suppose Joe (the book’s owner) wants to move to the next page from the current page: 
  • The book has a capability to move to the next page. 
  • Joe has to initiate (told the book to do) that action 
  • To do that, Joe has to know the name of the capability. Example: moveToNextPage 
  • After receive (and understand) the command, the book will move to the next page 
  • Alternatively, Joe can also told the book to move multiple pages forward. Example: moveForward + numberOfPage

From the previous scenario, we know the message’s call requires:
  • sender: Joe --> object initiating the action
  • receiver: The Book --> object whose method is being called
  • message name: moveToNextPage --> name of the method being called
  • parameters ( optional ): number of page --> extra information needed by the method to operate
  • return value ( optional ): --> receiver can reply an returning some information to the sender

Sumber

Slide PBO : Kelas, Objek, dan Attributes

Post a Comment

Lebih baru Lebih lama