1. Object Oriented Programming
02b: Classes
Informatics Department –Parahyangan Catholic
University
Taken and adapted from Lionov’s Lecture Slide
AUGUST, 24, 2017
2. Class
o It’s time to code the object into a something that will be understood
by a computer (a software object)
o But, there is a problem: We may not want to describe each individual
object because there might be too many objects.
o Idea : Several objects may have many commonalities. Why don’t we
describe what is common just once?
3. Class
o We can group the objects that we
have identified based on their
properties & capabilities.
o 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.
5. Class (3)
o In OOP, a cookie cutter is something we called a Class
o Class is a blueprint for objects with similar properties & capabilities.
o Class can only define what properties & capabilities an object will
have. A class cannot store any values. Only object can store any
values.
o Therefore, we only need to define a single class to describe the
properties & capabilities of similar objects.
o Class also defines a template for making an actual object. A class also
defines the initial values for the objects’ properties.
6. Class in Java Programming
Language
Java Programming Language:
o History of Java Language
o Java API (Application Programming Interface) provides almost 4000
classes that we can use
o Cross platform is one of the main advantages of Java PL (achieved
using Java Virtual Machine)
o Philosophy : "Write once, run anywhere"
7. Class in Java Programming
Language (2)
The Philosophy : "Write once, run anywhere":
8. Class in Java Programming
Language (3)
The Implementation Phase, using Java Programming Language and Java
Virtual Machine:
9. Class in Java Programming
Language (4)
Let us see a demo example: Simple Calculator
Assume that you’ve done all the steps:
1. Solve the problem / build your algorithm
2. Write the source code: Write the class using
Java PL and store it on a text file (.java)
3. Compile the source code using JDK, we will
get the Java bytecode (.class)
4. We run the bytecode in Java Virtual
Machine (JVM will interpret the bytecode)
10. Class in Java Programming
Language (5)
BLUE J:
o BlueJ is an IDE (Integrated Development Environement) for Java
Programming Language
o Although it is designed for educational purpose, we can use it to
develop a small-scale software
o We will use more sophisticated IDE like Netbeans, once you already
have an adequate programming skill
o BlueJ is free, but we have to install Java SDK separately (which is also
free)
11. Class in Java Programming
Language (6)
The structure of a class in Java :
public class ClassName{
}
12. Class in Java Programming
Language (7)
o ClassName can be changed into any name we want.
o the Java code to complete the creation of a class must be inserted
between the curly brackets ({ and })
o An example in BlueJ . . .
o . . . and an example of syntax error
13. Class in Java Programming
Language (8)
Identifier:
is a name assigned for classes, methods or variable
o First character must be a letter or underscore, but the rest is free
(including number and dollar sign)
o Spaces or symbols (? / %) cannot be used
o Several words cannot be used because Java use it as reserved words.
Example: new, class, extends, etc
o Exercises . . .
14. Class in Java Programming
Language (9)
Rules of naming for classes:
o Class name should be a noun(s)
o The first letter and also the first letter of each internal word should
be capitalized
o Avoid acronyms and/or abbreviations that leads to confusion. Bottom
line: when somebody read it, he/she must instantly understand what
the class is.
o Please do not be lazy to put several character !!
o Example: Ball, TabletPC, DoubleBed
15. Class in Java Programming
Language (10)
It is important to include several information into the source code :
Example in BlueJ . . .
16. 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
Example in BlueJ . . .
17. Software Objects (2)
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
18. Attributes & Methods
In Object Oriented Programming, we create a class and
◦ implement properties as instance variable a.k.a. Attributes
◦ implement capabilities as methods
19. Attributes & Methods (2)
o Each software object must communicate with others to accomplish
tasks
o They send messages (or instruction) to one another to invoke others
capabilities or change properties.
o 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.
20. Attributes & Methods (3)
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
21. Attributes & Methods (4)
From the previous scenario, we know the message’s call requires:
o sender: Joeobject initiating the action
o receiver: The Bookobject whose method is being called
o message name: moveToNextPagename of the method being called
o parameters (optional): number of page extra information needed
by the method to operate
o return value (optional): receiver can reply an returning some
information to the sender