OOP Concepts In Java

NIMESHIKA LAKMALI
3 min readJul 20, 2021

What is OOP?

An object-Oriented Programming System is the programming technique to write programs based on real-world objects. The states and behaviours of an object are represented as the member variables and methods. In OOPS programming programs are organized around objects and data rather than action and logic.

· It follows a bottom-up approach.

· It provides data hiding.

· It allows reusability of code.

· More flexibility.

Inheritance

Inheritance is one in which a new class is created that inherits the properties of the already existing class. It supports the concept of code reusability and reduces the length of the code in object-oriented programming.

Types of Inheritance are:

· Single inheritance

· Multi-level inheritance

· Multiple inheritances

· Hybrid inheritance

· Hierarchical inheritance

If you don’t want other classes to inherit from a class, use the final keyword:

Polymorphism

Polymorphism is that in which we can perform a task in multiple forms or ways. It is applied to the functions or methods. Polymorphism allows the object to decide which form of the function to implement at compile-time as well as run-time.

Types of Polymorphism are:

· Compile-time polymorphism (Method overloading)

· Run-time polymorphism (Method Overriding)

Encapsulation

The meaning of Encapsulation is to make sure that “sensitive” data is hidden from users.

To achieve this, you must:

  • declare class variables/attributes as private.
  • provide public get and set methods to access and update the value of a private variable.
  • Better control of class attributes and methods
  • Class attributes can be made read-only (if you only use the get method), or write-only (if you only use the set method)
  • Flexible: the programmer can change one part of the code without affecting other parts.
  • Increased security of data.

Abstraction

Abstraction means using simple things to represent complexity. Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. In Java, abstraction means simple things like objects, classes, and variables that represent more complex underlying code and data. This is important because it lets us avoid repeating the same work multiple times.

Ex:- We all know how to turn the TV on, but we don’t need to know how it works in order to enjoy it.

Summary

In this article, I described the OOP concepts in Java with examples. These matters may help to improve your basic object-oriented programming knowledge.

References

[1] “w3schoool,” [Online]. Available: https://www.w3schools.com/java/. [Accessed July 2021].

[2] “Geeksforgeeks,” [Online]. Available: https://www.geeksforgeeks.org/difference-between-inheritance-and-polymorphism/. [Accessed July 2021].

--

--

NIMESHIKA LAKMALI

Undergraduate at University of Colombo School of Computing