Professional Documents
Culture Documents
1
Features of OOP
Features of OOP
Course Module
Java Objects
Java is an object-oriented programming (OOP) language. But what on earth is
an OOP? An OOP is heavily focused on objects – which are the key to
understanding the concept of object-oriented programming.
Look around you – you’re surrounded with real-life objects – desks, chairs,
the Computer teacher/mentor, door, laptop/computer, iPhone, Android
tablet, Persian cat, lizard, switch, etc.
Each of these objects has different characteristics and behaviors.
Take the following for example:
iPhone (Object)
Characteristics: Name, Brand, Color, Size
Behavior: Charging, Updating, Turn Off, Turn On, Lock Screen
Switch (Object)
Characteristics: On, Off
Behavior: Turn On, Turn Off
Those are sample of real-life objects. Java objects are somewhat similar to the
real-life ones. They also have characteristics (or variables in Java) and
behaviors (called methods in Java).
Creating Objects
The syntax in creating an object is as follows:
<class name> <variable name> = new <class name>(parameters);
Example:
Point originOne = new Point(23, 94);
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Rectangle rectTwo = new Rectangle(50, 100);
Using an Object
Object fields are accessed by their name. You must use a name that is
unambiguous.
You may use a simple name for a field within its own class. For example, we
can add a statement in a class named Rectangle that displays the width
and height:
The code that is outside the object's class must use an object reference or
expression, followed by the dot (.) operator and a simple field name, as in:
objectReference.fieldName
Example:
We have an object named Rectangle with defined fields width and height.
//Instantiate the Rectangle object
Rectangle r = new Rectangle;
Features of OOP
Java has four fundamental object-oriented programming concepts – (1)
encapsulation, (2) inheritance, (3) polymorphism, and (4) abstraction.
Encapsulation
Encapsulation is a mechanism of packing up the data (variables) and the code
acting on the data (methods) together as a single unit. In encapsulation, the
variables of a class will be hidden from other classes. The hidden variables
can be accessed only through the methods of their current class. This process
is also known as data hiding.
For example, you discovered that you only have one piece of chocolate wafer
left. What do you do? You hide it in the crisper to prevent your younger
brother from eating it. It’s your private property, you know?
Course Module
To do this, you have to:
1. Declare the class variables as private.
2. Use public getter and setter methods to change and view the values of
variables.
Example:
Figure 1. Encapsulation
Inheritance
Inheritance can be defined as the process where one class acquires the
methods and fields of another class. Using inheritance, the information is
made manageable in a hierarchical order.
The class which inherits the properties of another is known as subclass
(derived class, child class). On the other hand, the class whose properties are
inherited is known as superclass (base class, parent class).
In other words,
a child class inherits from the parent class.
Course Module
Example:
“When the great King perishes, the Prince will inherit all of his belongings.”
Therefore, the Prince will own everything that his father had – palaces, cows,
altars, museums and other territories.
Given that the King’s treasures will be inherited directly by the Prince and a
method to get the value of the TREASURE, then we will use the code below.
Give it a try!
Polymorphism
Do you know Morphling from Defense of the Ancients? How about this hero’s
ability? (Flexible abilities)
Not playing DoTA? Okay, let’s talk about Biology. Polymorphism occurs when
there are different forms of an individual in the same species.
Take a lion and a lioness for example – they belong to the same species but
they look different. Or even us, humans. Boy and girls are of the same specie s
but we have different traits – physically and emotionally.
The same goes with Java programming language polymorphism – it is the
ability of a Java object to take different forms. Subclasses or child
classes could have different behaviors and still share the same functions
from their parent class or superclass.
A Java object that can pass more than one IS-A test is considered to be
polymorphic. All Java objects are polymorphic since any object will pass the
IS-A test for their own type and for the class Object.
It is important to know that you can only access an object using a
reference variable. A reference variable can be of only one type. Once
declared, the reference variable type cannot be changed.
Example:
public interface Vegetarian{}
public class Animal{}
public class Horse extends Animal implements Vegetarian{}
Course Module
Now let’s try a sample code in Java.
Create a class named Person with the following code:
If you run the Dancer class, you should get the following output:
Now create another class named Singer and type the following code:
Course Module
Abstraction
You type “Potato Corner branches” in Google and hit the enter key. Google
displays different results from the search keyword you entered.
Do you even wonder what happens when you hit Enter and how Google
manages to find what you’re looking for? How did Google even manage to
find about 85,400 results in just 0.48 seconds?
The truth is, search engines hide the protocol or whatever is happening
behind that Search button and shows us only what we need.
The same can be applied in Java through abstraction. This refers to the
process of hiding the details of implementation and showing only the
necessary functions.
Course Module
In the given example, you cannot instantiate the Employee class directly
from your main class. Instead, you can instantiate the Intern class, and
access all the fields and methods from the Employee class.
You can observe that the constructor inside the Employee class is the
same name with the class itself.
Glossary
Constructor (n.) – a special type of Java method that is used to initialize a a
Java object.
References
Java T Point. Constructor in Java. Retrieved from:
http://www.javatpoint.com/constructor
Oracle. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/
Tutorials Point. Java. Retrieved from:
http://www.tutorialspoint.com/java/java_tutorial.pdf