II | Object Oriented Programming— i

Tuan Burah
Programming Paradigms
4 min readJan 30, 2021

The fundamentals of OOP | How Classes and Objects work? | Explained using Diagrams

What we will cover up,

  1. What is OOP?
  2. Why OOP?
  3. Objects
  4. Classes
  5. Example of Classes & Objects.

What is Object Oriented Programming (OOP)?

OOP is a concept of programming that organizes software design around data, rather than functions and logic. I have mentioned similar concepts of programming, also known as ‘paradigms’, in my first blog on What are Programming Paradigms? Do make sure you read that before moving on to learning Object Oriented Programming.

Why Object Oriented Programming (OOP)?

In todays world of digital revolution, every software you come across is made up of a structured file system that include beautiful front-end designs and efficient back-end codes. Together these give an unbreakable, secured, efficient with good performance and fully functional program. Most (I mean like every) software or program you use are built with OOP, which is of course as the name states “Object Oriented” - wrapped up objects. Arears where OOP is applicable are, user interface design, Real Time Systems, Simulation and Modeling, Object oriented databases, etc

In OOP, you will be creating objects that contain both data and methods.
Data here means the raw facts you will assign into variables inside an object.
Methods here are definition functions inside an object. This has several advantages over procedural programming:

  • OOP is faster and easier to execute
  • OOP provides a clear structure for the programs
  • OOP avoids repeatable codes which makes the code easier to maintain, modify and debug
  • OOP makes it possible to create full reusable applications with less code and shorter development time

OOP focuses on Objects and Classes for developers to manipulate, followed by the principles of Object Oriented namely,

  • Encapsulation
  • Inheritance
  • Abstraction
  • Polymorphism

Popular OOP languages are JAVA, C#, PHP, Python, C++, etc.

So, what’s the big deal in using this complex paradigm when we could use a procedural paradigm?

Believe me, nobody would stop you from writing your programs in one extremely long file with hundreds of functions and thousands of code lines.
If you, for once, follow the principles of object-oriented programming you will be able to produce reusable, maintainable, scalable clean code which would be very easy in the case of testing and debugging.

Class

The what?
A class is a template from which objects are created. It will hold the data (variables) and behaviors (methods) that are common to all objects. You have to follow the below structure in order to declare a class,

Access Modifiers: it can be a public, private, protected or default. These must be mentioned when ever you create a class.

Class keyword: the keyword ‘class’ has to be used to create a class.

Class name: followed by the keyword, you need to set a name to the class.

Superclass (optional): The name of the parent class must be given if it has any preceded by a keyword ‘extends’. A class can only have one parent.

Interfaces (optional): The list of interfaces must be given with a comma. A class can implement more than one interface.

Body: The body of the class is surrounded by the curly braces {}.

Objects

The what?
These are real world entities such as car, chair, computer, watch, etc. They can be a state or a behavior, physical or logical.

“Objects are always defined as an instance of a Class.” — text books

In-depth
Objects takes some space in memory. A developer can create multiple similar objects and modify existing objects with a program/another object. Modifying objects can be explicitly allowed by the use of Encapsulation which I will explain in PART — iii.

Creating an Object

  • Declare
    First we have to declare the object name as a variable in the class.
  • Instantiate
    Second we instantiate the object by writing the “new” keyword inside the “main” method.
  • Initialize
    Third, we initialize the object by calling the constructor.

Examples of Classes and Objects

  • Simple example: Creating a user account for a social network.
    Consider the social network is object oriented, then there may exist a class defined as class userAccount with attributes such as, first name, last name, email, gender, age, password, profile image, martial status, etc.
    Without recreating these properties every time a user registers, a web script will simply instantiate theuserAccount as an Object with the same attributes, which will then be stored into a database.
  • Advanced example: Creating a character in a game.
    Lets consider the game called PUBG as a reference point. There may exist a class defined as class player with standard behaviors such as name, scope, move, attack. There may also be other classes to collect the behaviors like kill points, survival points, weaponry, inventory, etc.
    Here the class player is considered as the Parent Class and the other classes are considered as a Child Classes.
    Once we instantiate objects, both objects of Parent and Child classes can have their own attributes and methods as well as inherit them.
    For example there can be an object called Action with behaviors such as,Crawl(), Duck() and Stand() that calls the behavior of the parent object Attack() which would be common to all child objects.

--

--

Tuan Burah
Programming Paradigms

M.Sc. in IT (reading) | Pg.D. in IT | Student Member of British Computer Society — Royal Charter | Member of LivePhysics Science Community