Brief Introduction To Object Oriented Programming Design For Beginners
OOP vs Procedural Programming
I actually wanted to start this section with the title “why OOP?”. Then I realised making a comparison between Procedural Programming and OOP might be better to explain the merits of OOP over Procedural. Basically, the top down design approach is used in Procedural Programming. In this language such as C, a problem (a procedure) will be generally broken down into several sub problems (sub procedures), and those sub problems can be split into more sub problems until the big problem can be solved. Using this approach would cause hard maintenance of the code if the structure on the top of the hierarchy needs to be changed. In comparison, in OOP languages, maintaining the code would be much easier because the modules are based on an Object Oriented approach (code are separated into classes). But the tradeoff is that more code lines are needed than Procedural Programming.
The examples are given in Java language, and here are some key points in OOP.
Class & Object
A class is a user defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
Class Body
A class body usually includes zero or more attributes to represent the properties of a object. In addition, one class can also have zero or more methods to represent the behaviours of the corresponding object.
Attributes
Attributes indicate what properties that a object has. For example, a “Human” object could have attributes such as “arms”, “legs”, “body” and “brain” etc. A car object could have attributes such as “wheels” and “chassis”. Naming a attribute of an object should be a noun rather than a objective or a verb. For instance, A “Human” object has attributes like “beautiful” or “run” would sounds inappropriate.
Methods
Methods are the actions that a object can perform. For example, a “Human” object can perform actions such as “jump”, “run” and “shout”. Hence, the method name should be a verb rather than “hair”, “nose” or “legs”.
Abstraction & Encapsulation
abstract class Robot
{
private String name;
public void greeting()
{
System.out.println("Hi, I am a robot");
}
abstract void move();
public String getName()
{
return name;
}
public void setName(String name)
{
if (!name.equals(""))
this.name = name;
else
throw new IllegalArgumentException("Name cannot be Empty");
}
}
A abstract class could have zero or more abstract methods. The abstract method must be implemented when creating instances. Encapsulation means attributes of a class shall be defined as “private” rather than “public”. This protects the attribute to be “safe”. In the example of above code, in Robot class, the “name” attribute is encapsulated as private. When calling the setter “setName”, this code will throw an “IllegalArgumentException” exception when trying to pass an empty string as an argument. Now you might wonder what will happen if we do not encapsulate the attributes. Imagine if “name” is a public variable, then we can set the name to an empty string by “object.name = "";
” without accessing through “setName” method. And this could be problematic when the name attribute is not expected to be empty.
Inheritance & Polymorphism
class CatBot extends Robot
{
@Override
public void move()
{
System.out.println("moving like a cat");
}
}
class HumanBot extends Robot
{
@Override
public void move()
{
System.out.println("walking like a human");
}
}
As the code shown above, there are two classes (“HumanBot” and “CatBot”) inherited from Robot class. That means they have some same behaviours such as the method “greeting”. However they implement the “move” method differently, which is the essence of polymorphism. Polymorphism basically means there are several classes inherited from the same class, but they behave differently. Another example would be, class “Bird” and class “Tiger” are inherited from “Animal” class. But obviously they are different animals which indicate polymorphism.
Additional Notes
The basic features of OOP are briefly introduced above. However, Java is a elegant language worth exploring. Questions such as “What are interfaces in Java?”, “Does Java support multiple inheritance?” and “What is the diamond problem? and how Java handles it?” need you to discover the answers.
If you have any question about this blog, please leave a comment.