OOP Fundamentals (1 of 3)

Object Oriented Programming Fundamentals

OOP is a paradigm to solve real-world business problems by dividing the problem into a set of objects communicating with each other.

An Object consists of attributes and actions to be performed on the attributes. In programming jargon, attributes are called data members and actions are called member functions. Member functions are also referred as methods in Java.

In Java, Class is a type of objects which have same attributes & methods. In other words, a class is definition of attributes and methods. Once we have a class definition or blueprint ready, we can realize the class to create Objects. This realization is called instantiation in programming jargon.

Although Classes are blueprint and Objects are the instances of class, but the term ‘Object‘ is generally used to represent either of them.

Here’s an example of Account class and two of its instances having id 1010 and 1011.

Account class

Encapsulation and Abstraction

A class encapsulates data members and member functions into a single reusable entity. Re-usability of classes a benefit of using OOP; a class once written & tested can be re-used in different scenarios to solve similar business problems.

Abstraction is hiding internal details while exposing only relevant & required information to outside world. In java, abstraction is achieved by use of access specifiers. Here we are introducing only two of four access specifiers in Java –

Public – The entity (class, data member, method) is accessible to everyone.

Private – The entity is accessible to only the Class which defines it.

Let’s take real world example of a Car – which is made-up of a lot of attributes like engine, steering, brake pedal, gear-box, tyres, brake shoe, axle, etc. Car exposes interface to the user (methods) like changing gear, applying brake, etc. without telling the user that there is a brake shoe. Someone driving a car does not need to know that it has brake shoe but they need to know how to apply brake.

The benefit to using abstraction is that Car manufacturer can actually switch a mechanical brake with a hydraulic brake without any impact on the way people use or apply brake.

A software world example can be a AlarmLogger class – whose job is to write alarms in a file using a specific format. This class exposes a public method logAlarm and hides internal file-handing and formatting from clients calling this class by keeping them as private. This same class can be reused in multiple Programs like ecommerce app, banking app.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s