Programming Basics - Introduction To Object Oriented Programming


Object oriented programming (OOP) is all about thinking in terms of objects. Let’s dig deeper.

You will learn

  • What Is Structured Programming?
  • What Is Object Oriented Programming?
  • How is Object Oriented Programming different from Structured Programming?
  • How can you think in terms of objects - state and responsibility?
  • We look at a few examples of designing object oriented programs.

Before we discuss OOP, let us first look at structured programming.

What Is Structured Programming?

If you have ever done any programming in languages such as C and Pascal, then you have done what is called procedural, or structured programming. Such programming involves thinking in terms of procedures (or methods/functions). If you have a problem to solve, you split the logic of the solution into a sequence of multiple procedures, as the steps. You would start thinking about what functions need to be written.

Example: Flight Problem

Suppose the problem at hand is flying from one city to another. The different steps involved in that would take the form of a sequence of procedures as shown below:


	// Global Data

	fly() {
		travelToairport();
		findCheckInCounter();
		checkIn();
		passSecurityCheck();
		waitForBoardingCall();
		boardTheFlight();
		wishTheAirHostess();
		takeOff();
		haveFun();
		landing();
	}

We are looking to combine the functions that we write, to solve the stated problem.

What Is Object Oriented Programming?

Object Oriented Programming (OOP) brings a new thought process to the table. OOP focuses instead on the actors in the system called objects. It focuses on:

  • The data that these objects contain
  • The actions that can be performed on them

If this sounds complex to you, then you are not alone!

Every new programmer who is introduced to OOP feels pretty much the same way.

How would OOP approach the problem of taking a flight from one city to another?

Example: Flight Problem Revisited

Lets take a step by step approach:

Step-1 : Identify Objects

We first think about the different objects involved in the solution.

  • Aeroplane
  • Airhostess
  • Passenger

In addition to these, we could have more objects such as:

  • Cab
  • Airport
  • Co-passengers
  • Pilot

and many more.

Step-2

Once you have identified the objects of the system, you next determine the data an object would contains and actions that can be performed on it.

Aeroplane

  • Data : airline, make, type, position
  • Actions : takeOff(), land(), cruise()

Airhostess

  • Data : name, address
  • Actions : wish(), serve()

Passenger

  • Data : name, address
  • Actions : takeCab(), checkIn(), walk(), smile()

Of course, there could be a lot more data and actions in each of the objects that we have elaborated on, above.

In summary, structured programming is only concerned about identifying procedures, and combining them to form a solution. In contrast, OOP focuses on the objects involves, and deals with their individual data and actions.

Understanding OOP Terminology

Let’s now deal with a few standard terms that are used, when dealing with OOP.

Class

A class is nothing but a template used to create objects within a program. A class allows you to define the data that can be stored inside the object, as well as what actions can be performed on them. What you see below is the definition of a simple class, named Planet:


	class Planet {
		name, location, distanceFromSun //state
		revolve(), rotate() // behavior
	}

Object

An object is defined as an instance of a class. For example, earth and venus are instances of the Planet class. Here is how you go about creating Planet instances:

	Planet earth = new Planet();
	Planet venus = new Planet();

You can have other instances as well. If Planet defined the attributes of a planet, then earth and venus are live instances of planets.

State

The data that an object might contain is also referred to as its state. These are represented by the member variables inside the class.

The state of an object can change over a period of time.

The position of an Aeroplane at this point of time, would be different from what it will be after say, an hour’s time. Similarly, the distance of the earth from the Sun changes with the passage of time.

Behavior

The actions that can be performed on an object are referred to as its behavior. This translates to the methods that can be defined inside a class.

Let’s Try Some Exercises

Let’s now apply what we have learned so far, by applying OOPs to a couple of exercises.

Exercise 1 : Online Shopping System

Think about the different things that are involved in creating an online shopping system. Make sure to focus on their data, and enumerate what actions can be performed on them.

Exercise 2 : Person

Think about the different things that are related to a Person. Make sure to focus on the data that such a class stores, and enumerate what actions can be performed on a Person.

Solutions To Exercises

Exercise 1 : Online Shopping System

Customer

  • Data : name, address
  • Actions : login(), logout(), selectProduct(product)

Shopping Cart

  • Data : items
  • Actions : addItem(item), removeItem(item)

Product

  • Data : name, price, quantityAvailable
  • Actions : order(), changePrice()

Exercise 2 : Person

  • Data - name, address, hobbies, work
  • Actions - walk(), run(), sleep(), eat(), drink()

The idea behind these exercises was to get you thinking about applying concepts in OOP.

Do check out our video on the same topic:

image info

Summary

In this article, we had a introductory look at object oriented programming, or OOP. We first saw the approach taken by structured programming, that focuses on procedures. OOP brings a different approach by focusing on objects involved, and their data and actions. We introduced some OOP terminology, such as class, object, state and behavior. Finally, we explored a couple of exercises to try and apply the simple concepts of OOP that we saw here.

Congratulations! You are reading an article from a series of 50+ articles on Spring, Spring Boot , Hibernate, Full Stack, Cloud and Microservices. We also have 20+ projects on our Github repository. For the complete series of 50+ articles and code examples, click here.

Join 300,000 Learners!

Learn Spring Boot in 10 Steps - FREE Course

Next Steps

Image

Image

Image Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Image

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Spring and Spring Boot Video Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. Here's a list of video tutorials and courses for you

Software Design - Separation Of Concerns - with examples

Software architects and programmers love having Seperation of Concerns. What is it? Why is it important? Let's get started.

Object Oriented Software Design - Solid Principles - with examples

Software design is typically complex. Object oriented design takes it to the next level. There are a number of design patterns and other stuff to be aware of. Can we make things simple? What are the goals to aim for when you are doing object oriented design? SOLID Principles is a great starting point for Object Oriented Design.

Software Design - Open Closed Principle - with examples

Open Closed Principle is one of the SOLID Principles. You want your code to be easily extended. How do you achieve it with minimum fuss? Let's get started.

Software Design - What is Dependency Inversion Principle?

Dependency Inversion Principle is one of the important SOLID Principles. Dependency Inversion Principle is implemented by one of the most popular Java frameworks - Spring. What is it all about? How does it help you design good applications?

Introduction to Four Principles Of Simple Design

With agile and extreme programming, the focus is on keeping your design simple. How do you keep your design simple? How do you decide whether your code is good enough?

Software Design - Single Responsibility Principle - with examples

For me, Single Responsibility Principle is the most important design principle. What is Single Responsibility Principle? How do you use it? How does it help with making your software better? Let's get started.

REST API Best Practices - With Design Examples from Java and Spring Web Services

Designing Great REST API is important to have great microservices. How do you design your REST API? What are the best practices?

Designing REST API - What is Code First Approach?

Designing Great REST API is important to have great microservices. Code First approach focuses on generating the contract from code. Is it the best possible approach?