DEV Community

Ruzanna Frangulyan
Ruzanna Frangulyan

Posted on

Java as an OOP language

Before starting to explore Java as OOP, let’s revise some stuff that you watched in my YouTube video.

We have two main types in Java. Primitive and Non-primitive.

A primitive data type specifies the size and type of variable values, and it has no additional methods.

Image description

There are five types of non-primitive data types in Java. These are user-defined data types created by programmers, which are used to store multiple values.

Image description

Whenever a non-primitive data type is defined, it refers to a memory location where the data is stored in heap memory, so it relates to the memory location where an object is placed.
This was a quick revision of Types in Java.

Now, let’s understand Java as an OOP language.
In total, we have five types of programming languages.

Image description

We will concentrate today on OOP as Java is the 3rd type of language in our list.
So why OOP?
This type of language treats a program as a group of objects composed of data and program elements, known as attributes and methods. Objects can be reused within a program or in other programs. This makes it a popular language type for complex programs, as code is easier to reuse and scale.

There are four pillars of OOP.

Image description

1. Abstraction.

In programming, abstraction hides complex inner workings and only shows the essential parts. It helps to describe things in a simple way and hine unnecessary stuff from the “user”
In our kitchen example, you can understand abstraction as Following a Recipe without needing to understand how each ingredient is produced. Developers focus on using functionalities without delving into implementation details, as a chief, who uses, for example, salt, without worrying about its chemical composition.

2. Encapsulation

It is the practice of bundling the data and methods that operate on the data into a single unit called a class.

In Java, there can be private and public classes. The main difference between them is that public classes can be accessed to anywhere in the program, while private ones are hidden and are accessible only in the specific class.

Encapsulation involves marking certain variables as private and hiding the internal workings and implementation details of the class, allowing changes to be made within the class without affecting the code that uses the class.
Encapsulation in a kitchen or restaurant is similar to having ingredients stored in labeled containers and utensils arranged in drawers or cabinets. Just like in cooking, where ingredients are kept organized and accessible, It's about keeping the cooking process efficient by having everything in its place, accessible only when needed, and safeguarded from accidental misuse.

3. Inheritance

In the concept of inheritance, one class inherits the attributes and methods of another class. The class whose properties and methods are inherited is known as the Parent class. The class that inherits the properties from the parent class is the Child class.
In Java, it is the method of creating a connection between classes by inheriting from other classes.
In the same way, the cooking recipe can be inherited from one generation to another, but note that every generation is free to add its unique details to it every time it passes from parent to child, as in our case, classes.

4. Polymorphism

Polymorphism in Java is the task that performs a single action in different ways, as the word itself means “many forms”. Languages that do not support polymorphism are called 'Object-Based Languages'.
Polymorphism in Java can be compared to adapting a recipe to create different dishes by altering ingredients or cooking methods. Similarly, Java methods can behave differently based on their implementation.

I hope this post was helpful for you. Good luck in discovering the world of Java programming :)

Top comments (0)