Basics Concepts of Object Oriented Programming with Java

Alperen Yıldırım
4 min readJan 15, 2023

--

In this article, we will understand the basic concepts for OOP with code blocks and I’ll share related resources.

What is object-oriented programming?

OOP is a programming paradigm, or classification, that organizes a group of data attributes with functions or methods into a unit, known as an object.

Typically, OOP languages are class-based, meaning a class defines the data attributes and functions as a blueprint for creating objects, which are instances of the class.

One class may represent multiple independent objects, which interact with each other in complex ways. Popular class-based programming languages include Java, Python and C++. We will work with Java.

Structure of object-oriented programming

OOP contains various structures, known as the building blocks. These structures include:

  • Class: A class is a data type that provides a framework for creating objects. You can define a class to create multiple objects without writing additional code.
  • Object: In OOP, an object represents an instance, or creation, of a class. Objects define specific data, such as properties and behaviors, to implement code.
  • Method: A method is a function that performs a task or action. For example, a method may return information about an object’s data.
  • Attribute: This structure stores information about an object and defines its state. You can define an attribute as part of the class.
access_modifier class<class_name>
{
data member;
method;
constructor;
nested class;
interface;
}

4 principles of object-oriented programming

1. Encapsulation

Encapsulation means to enclose data by containing it within an object. In OOP, encapsulation forms a barrier around data to protect it from the rest of the code.

You can perform encapsulation by binding the data and its functions into a class. This action conceals the private details of a class and only exposes the functionality essential for interfacing with it.

When a class doesn’t allow direct access to its private data, it’s well-encapsulated.

public class Student  {  
private String name; //private data member

public String getName(){ //getter method for name
return name;
}
public void setName(String name){ //setter method for name
this.name=name
}
}

2. Abstraction

Abstraction refers to using simplified classes, rather than complex implementation code, to access objects. Often, it’s easier to design a program when you can separate the interface of a class from its implementation.

In OOP, you can abstract the implementation details of a class and present a clean, easy-to-use interface through the class member functions.

Abstraction helps isolate the impact of changes made to the code so if an error occurs, the change only affects the implementation details of a class and not the outside code.

abstract class Animal {
public abstract void animalSound(); //abstact method of class
public void sleep() {
System.out.println("Zzz");
}
}

3. Inheritance

Most object-oriented languages support inheritance, which means a new class automatically inhabits the same properties and functionalities as its parent class.

Inheritance allows you to organize classes into hierarchies, where a class might have one or more parent or child classes.

If a class has a parent class, it means the class has inherited the properties of the parent. The child class can also modify or extend the behavior of its parent class. Inheritance allows you to reuse code without redefining the functions of a child class.

class Subclass-name extends Superclass-name  
{
//methods and fields
}

4. Polymorphism

Polymorphism refers to creating objects with shared behaviors. In OOP, polymorphism allows for the uniform treatment of classes in a hierarchy.

When you write code for objects at the root of the hierarchy, any objects created by a child class within the hierarchy have the same functions. Depending on the type of object, it may execute different behaviors.

class Bike{  
void run(){System.out.println("running");}
}
class Splendor extends Bike{
void run(){System.out.println("running safely with 60km");}

public static void main(String args[]){
Bike b = new Splendor();//upcasting
b.run();
}
}

Benefits of object-oriented programming

OOP is a popular programming style in many languages. By defining sets of classes that represent and encapsulate objects in a program, OOP can organize classes into modules, improving the structure of software programs.

It’s common for software developers to use OOP when creating complex programs because it allows them to define classes and their relationships. Other benefits of OOP include:

  • Reusable code: The inheritance principle of OOP allows you to reuse code without writing it repeatedly. This feature can help to reduce errors when creating code.
  • Increased productivity: By creating objects from classes, you can save time when developing new software. You can also use libraries and reusable code to increase your productivity.
  • Enhanced security: You can use encapsulation and abstraction to display limited data while concealing sensitive information. These features can provide enhanced security when developing complex code.

Please feel free to share your opinions on comments.
I hope to develop my writing skills and my main goal here is to increase knowledge by sharing.

Thank you for your time and happy learning! :)
Alperen

--

--

Alperen Yıldırım

Fresh Data Engineer - Marathon finisher and Triathlete | Data Engineering M.Sc. Student Love reading and writing about self-development, psychology, philosophy