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

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

What is an In Memory Database?

In this article, we understand what the term "In-Memory Database" actually means. We see what scenarios they can be used, and why they are important.

What is an Embedded Server?

In this article, we focus in exploring what an embedded server is, and how they help make application deployment simpler.

Java Tools and Frameworks - Introduction To Maven

In this article, we experience a brief introduction to Maven, and will see how it helps in managing your project dependencies.

Five Tools To Learn as a Java Developer

In this article, we introduce you to five great tools that we recommend every Java Developer to learn.

Five Languages To Learn as a Java Programmer

As a programmer, you want to explore new things. Learning a new language gives you a different perspective when solving problems. What are the 5 languages that a Java developer should explore?

Five Great Frameworks To Try for Java Programmers

As a programmer you want to explore new languages and frameworks. What would be the best frameworks to explore in 2019 for Java Programmers?

Unit Testing Best Practices - with Java and Junit Examples

Unit Testing is often underrated. As far as I am concerned, having good unit tests seperates good projects from bad. In this article, let's look at unit testing best practices in depth. We use examples from Java and JUnit to illustrate the concepts. However, the best practices are applicable irrespective of the programming language used.

Programming Basics - Unit Testing - What Is Mocking?

Mocking is an important concept that is made use of, when writing unit tests. In this article, we explore what mocking is all about, and explore some of the popular Java mocking frameworks in use.