Object-Oriented Programming Principles in Java: OOP Concepts for Beginners

Thanoshan MV
3 min readMay 1, 2020

--

Object-oriented programming offers a sustainable way to write spaghetti code. It lets you accrete programs as a series of patches.― Paul Graham

Fundamentals of object-oriented programming

Object-oriented programming is a programming paradigm where everything is represented as an object.

Objects pass messages to each other. Each object decides what to do with a received message. OOP focuses on each object’s states and behaviors.

What Are Objects?

An object is an entity that has states and behaviors.

For example dog, cat and vehicle. To illustrate, dog has its states like age, color, name and behaviors like eating, sleeping and running.

State tells us how the object looks or what properties it has.

Behavior tells us what the object does.

We can actually represent a real world dog in a program as a software object by defining its states and behaviors.

Software objects are the actual representation of real world objects. Memory is allocated in RAM whenever creating a logical object.

An object is also referred to an instance of a class. Instantiating a class means the same thing as creating an object.

The important thing to remember when creating an object is: the reference type should be the same type or a super type of the object type. We’ll see what a reference type is later in this article.

What Are Classes?

A class is a template or blueprint from which objects are created.

Imagine a class as a cookie-cutter and objects as cookies.

Figure 1: Illustrates class and object relationship through cookie-cutter and cookies. Source.

Classes define states as instance variables and behaviors as instance methods.

Instance variables are also known as member variables.

Class doesn’t consume any space.

To give you an idea about class and objects, let’s create a Cat class that represents states and behaviors of real world Cat.

public class Cat {
/*
Instance variables: states of Cat
*/
String name;
int age;
String color;
String breed;

/*
Instance methods: behaviors of Cat
*/
void sleep(){
System.out.println("Sleeping");
}
void play(){
System.out.println("Playing");
}
void feed(){
System.out.println("Eating");
}

}

Now, we have successfully defined a template for Cat. Let’s say we have two cats named Thor and Rambo.

Figure 2: Thor is sleeping. Source
Figure 3: Rambo is playing. Source

How can we define them in our program?

First, we need to create two objects of Cat class.

public class Main {
public static void main(String[] args) {
Cat thor = new Cat();
Cat rambo = new Cat();
}
}

Next, we’ll define their states and behaviors.

public class Main {

public static void main(String[] args) {
/*
Creating objects
*/
Cat thor = new Cat();
Cat rambo = new Cat();

/*
Defining Thor cat
*/
thor.name = "Thor";
thor.age = 3;
thor.breed = "Russian Blue";
thor.color = "Brown";

thor.sleep();

/*
Defining Rambo cat
*/
rambo.name = "Rambo";
rambo.age = 4;
rambo.breed = "Maine Coon";
rambo.color = "Brown";

rambo.play();
}

}

Like the above code examples, we can define our class, instantiate it (create objects) and specify the states and behaviors for those objects.

Now, we have covered the basics of object-oriented programming. Let’s move on to the principles of object-oriented programming.

These are the four main principles of the object-oriented programming paradigm. Understanding them is essential to becoming a successful programmer.

Principles of object-oriented programming

  1. Encapsulation
  2. Inheritance
  3. Abstraction
  4. Polymorphism

Now, we know there are four main principles of OOP. We’ll see them one by one. I’ve linked to them. You can access them.

I’ll meet you on the Encapsulation in Java article.

Thank you for reading.

Happy coding!

--

--

Thanoshan MV
Thanoshan MV

Written by Thanoshan MV

Hi! I'm a Software Engineer with a passion for research in AI, software engineering, and open-source development. Web - https://thanoshanmv.github.io/

No responses yet