Object-Oriented Programming Concepts in Java

Nimasha Madhushani
LinkIT
Published in
4 min readJan 26, 2024
image by author

OOP is a fundamental programming concept that includes the concepts of abstraction, encapsulation, inheritance, and polymorphism. When comparing earlier programming methods, Reusability, maintainability, and extensibility, of code are the main advantages of OOP concepts.

Ok…Now, Let’s move on with further clarifications.

Inheritance

image by author

Inheritance is a core concept of object-oriented programming and eliminates redundant code. Therefore, we can use inheritance for method overriding and code reusability. Sub class and super class are the two main important terms, we use in inheritance in Java.

Super class- base/parent class where a subclass inherits the features.

Sub class- an extended class that inherits the other classes.

Single Inheritance

image by author
image by author

Output

image by author

If we change the String brand; variable in the super class as private String brand; The sub class called NewPhone will not be allowed to access the variable called brand.

Hierarchical Inheritance

image by author
image by author

Output

image by author

Multilevel Inheritance

A class inherits properties from a class that again has inherited properties.

image by author
image by author

Output

image by author

Encapsulation

Encapsulation means the bundling of variables and methods inside a class. Furthermore, it can achieve data hiding, and getters and setters are provided. A completely encapsulated class can be created in Java by making all the class variables private. Data hiding, Increased flexibility, and Reusability are some advantages of encapsulation.

🔮🧸Animal Class

image by author

🐶Dog Class

image by author

🎯Demo Class

image by author

Output

image by author

Polymorphism

Having many forms is the simple definition of polymorphism. When considering the real world, one person can play different roles. For example, a person at the same time is a brother, father, husband, or friend. But, the behavior will be changed according to the situation. This is the meaning of polymorphism. There are two types of polymorphism

Compile time polymorphism(static polymorphism)

The method used to create this form of polymorphism is function overloading. Methods will be overloaded when there are multiple methods with the same name but different parameter lists(methods with different method signatures).

Runtime polymorphism(dynamic polymorphism)

It is a procedure by which a function call to an overridden method is handled at runtime. By using Method Overriding, this kind of polymorphism is accomplished. Method overriding is the process of declaring a method in a subclass that already exists in the parent class. Constructors, static methods, and final methods cannot be overridden. Overridden is allowed when the instance methods are inherited by the child class.

image by author
image by author

Output:

image by author

Polymorphism using method overriding

image by author

Output:

image by author

Polymorphism using method overloading

image by author

Output:

image by author

Abstraction

The process of hiding specific details and showing only essential information to the user is data abstraction. The abstract keyword (non-access modifier), is used for methods and classes. Abstract classes cannot be used to create objects; abstract methods can only be used inside an abstract class. But, non-abstract classes can be contained inside the abstract class. These abstract methods don’t have a body and the body is provided by the subclass in inherited form. Abstract classes should be extended and the abstract methods should be overridden.

image by author

Output

image by author

All 4 concepts of object-oriented programming have been discussed using examples and code snippets. I hope you gained some knowledge by going through this article.

Thank you…..!!😎

--

--