Understanding Object-Oriented Programming

Akash De AlwisAkash De Alwis
3 min read

One of the most important features of Java is that it is a powerful, versatile programming language that every student who wants to become a computer scientist needs to know. Among the most important things to understand about Java is its ability to support Object-Oriented Programming (OOP). In order for first year students embarking on their Java journey to master it at all, they must first understand OOP concepts. The basic understanding of OOP in Java will be discussed in this blog-post so that you can write good, clean, and easily maintainable codes.

What is even Object Oriented Programming (OOP) after all? Object Oriented Programming oriented is a programming model that is centered on the objects rather than actions. In OOP programming design is done using objects, which are instances of classes. Class outlines properties and behaviors (methods) that objects of such class will have.

Key Principles of OOP

  • Encapsulation

  • Inheritance

  • Polymorphism

  • Abstraction

1. Encapsulation

Encapsulation is the bundling of data (variables) and methods (functions) that operate on the data into a single unit called a class. It restricts direct access to some of the object's components, which is a means of preventing accidental interference and misuse of the data.

In this example, private variables are ‘age’ and ‘name’. These can only be accessed and modified using public methods (getName, setName, getAge, setAge).

2. Inheritance

Inheritance is a mechanism wherein a new class inherits the properties and behaviors of an existing class. The existing class is called the superclass, and the new class is called the subclass.

In this case, by inheriting from Person class, student manages to access the name property and getName method owned by Person class.

3. Polymorphism

Polymorphism means that positions of objects can often be taken as those of their parent class instead of being tied to their exact (actual) class type; it happens when one object behaves in different ways from others within the same group. Imagine a table containing various fruits such as bananas, apples, and grapes among others—these are all under the category “fruits” regardless of their differences because they carry these attributes together. In regard to polymorphism, those shapes which are specific like a square or triangle when they become shapes, which are general. In Java programming language, we use compile time and run-time polymorphisms when creating applications."

method overloading

method overriding

method overriding

4. Abstraction

Abstraction is a notion of concealing the complex nature of implementation of an object and showing only the object's essence. Java accomplishes abstraction by using abstract classes and interfaces.

abstract class

abstract class

interface

Conclusion

For creating of resilient Java programs it is necessary for every programmer to know OOP principles. Strong Java application development could only be assured on mastering encapsulation, inheritance, polymorphism and abstraction. Through using more often, the second skill will come more naturally than before while the third still remains elusive.

[Akash De Alwis]

[OOP concepts]

0
Subscribe to my newsletter

Read articles from Akash De Alwis directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akash De Alwis
Akash De Alwis

Undergraduate student 😊🤞