Object Oriented Programming (OOPs) Concept in Java

Object Oriented Programming (OOPs) Concept in Java 

 

Hello guys, welcome to my blog!

In this blog, I will guide you through essential OOP principles like inheritance, polymorphism, encapsulation, and abstraction, and demonstrate how they are applied in Java. Through practical examples, code snippets, and real-world applications, you’ll not only learn the theory behind OOP but also how to implement it effectively in your Java projects. 

Join me as we explore the power of OOP and enhance your programming skills!

 

What is Class? 

A class is a blueprint/template that is used to create individual objects. Using classes, you can create multiple objects with the same behavior instead of writing their code multiple times.

A class contains,
  • Attributes
  • Behaviors operations   

In general, class declarations can include these components in order: 

Modifiers: private, default, public, protected

Class Name: The class name should begin with a capital letter.

Body: The class body is surrounded by braces, { }.

 


Example:

 


What is Object? 

An object is an instance of a class. A typical Java program creates many objects, which as you know, interact by invoking methods. 

What is Method?

A method is a collection of statements that perform specific tasks and return a result to the caller.

Pillars of OOPS in Java

Let's now see the 4 pillars of OOPs:

1. Abstraction

Abstraction is the process of hiding non-essential details and showing only the essential details to the user.
  • Abstract classes must have at least one abstract method (methods with NO body) and can have normal methods as well.
  • Abstract classes cannot be used to create objects (to access it must be inherited from another class).
  •  Constructors cannot be created for abstract classes.

 

output:

barking

eating 

2.Encapsulation

Simply we can say encapsulation is bundling of variables and methods inside a class (to make sure that sensitive data is hidden from user).

  • In here, we use private variables/methods.
  • Use get and set methods to access those private variables/methods.

  

 

3.Inheritance

 

 

4.Polymorphism

 

Comments