Skip to main content

Important Concept of OOPs

Hello Everyone I am Raushan Ranjan

In this post we are going to cover the important concept of OOPs.

OOPs is an important key of programming language. We have to make our mind clear towards OOPs by understanding the each and every point given in this post.

Let's jump into the main part

OOP, or Object-Oriented Programming, is a way of writing code that helps us organize and manage our programs more effectively. 

In OOP, we work with two main things: objects and classes.

An object is something that represents a specific thing or concept in our program. For example, if we have a program about cars, an object could be a specific car like "Toyota Camry." Objects have characteristics or properties (like color, size, or speed) and can perform actions or behaviors (like accelerating or braking).

A class, on the other hand, is like a blueprint or template for creating objects. It describes what properties an object will have and what actions it can perform. It defines the common features and behaviors shared by all objects of that type. Going back to our car example, the class would define what a car is and what all cars can do.

OOP has four main principles or concepts: encapsulation, inheritance, polymorphism, and abstraction.

    1. Encapsulation means bundling related data (properties) and behaviors (methods) together in an object. It helps keep things organized and protects data from being accessed or modified by unauthorized parts of the code.

    2. Inheritance and its types (with code):
Inheritance is a mechanism in OOP that allows a class to inherit properties and methods from another class. The class that is being inherited from is called the superclass or parent class, and the class that inherits is called the subclass or child class. There are different types of inheritance, such as:

  • Single Inheritance: When a class inherits from only one superclass.

class ParentClass {
    public void parentMethod() {
        System.out.println("This is a parent method");
    }
}
class ChildClass extends ParentClass {
    public void childMethod() {
        System.out.println("This is a child method");
    }
}
// Creating an object of the child class
ChildClass obj = new ChildClass();

 obj.parentMethod();  // Accessing parent method 

obj.childMethod();  // Accessing child method

 

  • Multiple Inheritance: Java doesn't support multiple inheritance of classes. However, it can be achieved using interfaces, which allow a class to implement multiple interfaces.

 

interface Interface1 {
    void method1();
}
interface Interface2 {
    void method2();
}
class MyClass implements Interface1, Interface2 {
    public void method1() {
        System.out.println("Method 1"); 
    }
    public void method2() {
        System.out.println("Method 2");
    }
}
// Creating an object of the class
MyClass obj = new MyClass();
obj.method1();  // Accessing method from Interface1
obj.method2();  // Accessing method from Interface2

 

  • Hierarchical Inheritance: When multiple classes inherit from a single superclass.

 class ParentClass {

    public void parentMethod() {
        System.out.println("Parent Method");
    }
}
class ChildClass1 extends ParentClass {
    public void childMethod1() {
        System.out.println("Child 1 Method");
    }

class ChildClass2 extends ParentClass {
    public void childMethod2() {
        System.out.println("Child 2 Method");
  }
}
// Creating objects of the child classes
ChildClass1 obj1 = new ChildClass1();
ChildClass2 obj2 = new ChildClass2();
obj1.parentMethod();  // Accessing parent method from ChildClass1
obj2.parentMethod();  // Accessing parent method from ChildClass2
obj1.childMethod1();  // Accessing method from ChildClass1
obj2.childMethod2();  // Accessing method from ChildClass2

 


    3. Polymorphism

Polymorphism is the ability of objects to take on different forms based on the context. It allows objects of different classes to be treated as objects of a common superclass. Polymorphism can be achieved through method overriding or method overloading.

  • Method Overriding: When a subclass defines a method with the same name as a method in the superclass, it is said to override the superclass's method.

class ParentClass {

    public void showMessage() {

        System.out.println("Parent class message");

    }

}

class ChildClass extends ParentClass {

    public void showMessage() {

        System.out.println("Child class message");

    }

}

// Creating objects of both classes

ParentClass parentObj = new ParentClass();

ChildClass childObj = new ChildClass();

parentObj.showMessage();  // Accessing parent class method

childObj.showMessage();  // Accessing child class method

  • Method Overloading: When a class has multiple methods with the same name but different parameters, it is said to have overloaded methods.

class MyClass {

    public int sumNumbers(int a, int b) {

        return a + b;

    }

    public int sumNumbers(int a, int b, int c) {

        return a + b + c;

    }

}

// Creating an object of the class

MyClass obj = new MyClass();

System.out.println(obj.sumNumbers(1, 2));  // Calling with two arguments

System.out.println(obj.sumNumbers(1, 2, 3));  // Calling with three arguments


    4. Abstraction (with code):

Abstraction is the process of hiding unnecessary implementation details and exposing only essential features to the user. In Java, abstraction can be achieved using abstract classes or interfaces.

abstract class AbstractClass {

    public abstract void abstractMethod();

}

class ConcreteClass extends AbstractClass {

    public void abstractMethod() {

        System.out.println("Implementation of abstract method");

    }

}

// Creating an object of the concrete class

ConcreteClass obj = new ConcreteClass();

obj.abstractMethod();  // Accessing the abstract method implementation


Constructor:
A constructor is a special method that is automatically called when an object is created. It is used to initialize the object's attributes or perform any other setup operations. In Java, the constructor method has the same name as the class.


class MyClass {
    private String name;

    public MyClass(String name) {
        this.name = name;
    }

    public void greet() {
        System.out.println("Hello, " + name + "!");
    }
}

// Creating an object of the class
MyClass obj = new MyClass("John");
obj.greet();  // Accessing the greet method


I hope this provides a simple and clear understanding of these concepts in Java! That's all for this post please read it carefully and leave a comment if you face any problem. 

Comments

Popular posts from this blog

                              INCOME IDEAS                                                                          by RAUSHAN RANJAN PROBLEMS: ➨Don't have better life? ➨ Are u broke? ➨ Mentally tensed? ➨ Feeling useless? MONEY IS SOLUTION. World  is growing at faster rate. Some get aware of this growing rate at early age. Some get aware late.Getting aware means come to know that we have  to do lot of hard work to give ourself a better life. The question is why we need to work hard. And the answer is very simple  "TO EARN MONEY".Because money is attracted towards hardworking people. Hardworking is an act of respect towards money. ELON MUSK We can't neglect the fact that money can't bu...

Understanding Array In Detail

Hello everyone I am Raushan Ranjan In this post we are going to get the details of Array. We will discuss everything about array. Let's understand in detail An array is a data structure that allows you to store multiple values of the same type under a single variable name. It provides a way to organize and access a collection of elements using a contiguous block of memory. In most programming languages, arrays are zero-indexed, which means the first element is accessed with an index of 0, the second with an index of 1, and so on. The size or length of an array determines the number of elements it can store. Here's an example of declaring and initializing an array in different programming languages: C++ : int numbers[5]; // Declaration of an integer array of size 5 int numbers[] = {1, 2, 3, 4, 5}; // Declaration and initialization of an integer array with values numbers[0] = 10; // Assigning a value to the first element int x = numbers[2]; // Accessing the third element and sto...