Object-Oriented Programming (OOP)

Category : Java | Sub Category : Object- oriented Programming | By Prasad Bonam Last updated: 2023-07-31 06:28:03 Viewed : 70

Object-Oriented Programming (OOP) is a programming paradigm that focuses on organizing code into objects, which encapsulate data and behavior. Java is an object-oriented programming language, and it follows several OOP principles. Here are the main OOP concepts in Java:

  1. Class: A class is a blueprint for creating objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have.
class MyClass { // Properties (attributes) int myInt; String myString; // Constructor public MyClass(int myInt, String myString) { this.myInt = myInt; this.myString = myString; } // Method public void printInfo() { System.out.println("MyInt: " + myInt + ", MyString: " + myString); } }
  1. Object: An object is an instance of a class, created using the new keyword. It represents a real-world entity and has its own state and behavior.
MyClass myObject = new MyClass(42, "Hello");
  1. Encapsulation: Encapsulation is the process of hiding the internal implementation details of an object and exposing only the necessary functionalities through public methods. It helps achieve data hiding and protects the objects state from unauthorized access.
class BankAccount { private double balance; // Getter and Setter methods public double getBalance() { return balance; } public void setBalance(double newBalance) { if (newBalance >= 0) { balance = newBalance; } } }
  1. Inheritance: Inheritance is a mechanism that allows a class (subclass) to inherit properties and behaviors from another class (superclass). It promotes code reusability and establishes an "is-a" relationship.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
class Dog extends Animal {
// Inherits sound() method from Animal class
  1. Polymorphism: Polymorphism means having multiple forms. In Java, polymorphism can be achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).
  • Method Overloading: Same method name but with different parameters in the same class.
class MathOperations { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } }
  • Method Overriding: Overriding a superclass method in a subclass with the same method signature.
class Shape { void draw() { System.out.println("Drawing a shape"); } } class Circle extends Shape { @Override void draw() { System.out.println("Drawing a circle"); } }
  1. Abstraction: Abstraction focuses on defining the essential features of an object while hiding unnecessary details. Abstract classes and interfaces are used to achieve abstraction in Java.
abstract class Shape { // Abstract method (no implementation) abstract void draw(); // Concrete method (with implementation) void displayInfo() { System.out.println("This is a shape."); } }
  1. Association, Aggregation, and Composition: These concepts define relationships between objects.
  • Association: A simple relationship between objects, where one object knows about the other.

  • Aggregation: A stronger relationship where one object contains another object, but they can exist independently.

  • Composition: A stronger form of aggregation where one object is composed of other objects, and they cannot exist independently.

These are the core OOP concepts in Java. Understanding and applying these concepts properly can lead to more organized, maintainable, and scalable code.

Related Articles

Leave a Comment: