Object-Oriented Programming(OOPs) concept in java

Java Object-Oriented Programming is a powerful way to approach
ch the task of programming. OOPs refers to languages that use objects in programming. To understand the concept of OOPs needs to know the fundamental terms and concepts. Oops, we use three concepts of fundamentals for the programming language. Such as Classes, Objects, methods. Additionally, abstraction, encapsulation, inheritance, etc.

The general concept Of Java Object-Oriented Programming

Object-Oriented Programming

1.Objects:

Objects are the key to understand the OOP approach. As they are small, self-contained, and modular. An object is a real-world entity. Every object consists of an Attribute or state and behavior. An attribute is a possible condition that an object can exist in and represent by its characteristics or data.

For example

A cat is an object which has attributes, Like a name, age, height, etc. The behavior of an object determines how the object acts or behaves. They are represented by the operation that it can perform. For example, A cat is an object that can move, walk, etc.

Object-Oriented Programming

2. Class:

A class is a user-defined data type that contains the entire set of similar data and the functions that the objects own. In other words, we can say a class is a group of similar objects. Once, specified the class, we can create many objects belonging to that class.
For example, a car is a class, and sonata, BMW, Camry are their objects.

Object-Oriented Programming

3. Abstraction:

Abstraction refers to the act of representing essential features without including the background details. So that one can focus on important things at a time. In Java Object-Oriented Programming, complexity can manage by using the concept of abstraction. Let’s consider an example of Television to understand the abstraction concept.
For example:
Television consists of features such as changing the channel, volume control, On/OFF switch, etc. Users interact with television using interfaces, Like channel changer, volume controller, and power button without going into internal details of the working of each of the interfaces. The user is not concerned about how it receives signals over the air and translates them display on the screen. So it shows how the internal complexity of working of the television, hidden from the screen.

Object-Oriented Programming

4.Encapsulation:

Encapsulation is a mechanism of binding the data and methods that operate on them together in a single unit called class. It keeps the data safe from any external interference and misuse. It is a way to install data abstraction. Encapsulation provides services to the external function or other objects that interact with it. But, these external functions or objects do not need to know their internal detail.

Object-Oriented Programming

5.Inheritance:

Inheritance is the process of deriving a new class from the existing class without modifying it. The new class defines as the subclass or derived class or child class and the existing class is called the superclass or base class or parent class.
Important terminology:

  • Super Class: The class whose features are inherited, known as superclass(or a base class or a parent class).
  • Sub Class: The class that inherits the other class is known as a subclass (or a derived class, extended class, or child class). The subclass can add its fields and methods besides to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Object-Oriented Programming

6. Polymorphism:

Polymorphism is a very powerful concept. The word polymorphism is derived from two Greek words. Poly means many and morphos means forms. So, polymorphism means the ability to take many forms. In other words, Polymorphism defines as the ability to use the same name for two or more related but technically different tasks.

For example:

class Animal {
  public void animalSound() {
    System.out.println("The animal makes a sound");
  }
}

class Pig extends Animal {
  public void animalSound() {
    System.out.println("The pig says: wee wee");
  }
}

class Dog extends Animal {
  public void animalSound() {
    System.out.println("The dog says: bow wow");
  }
}

class MyMainClass {
  public static void main(String[] args) {
    Animal myAnimal = new Animal();
    Animal myPig = new Pig();
    Animal myDog = new Dog();
        
    myAnimal.animalSound();
    myPig.animalSound();
    myDog.animalSound();
  }
}

As a Result: Output:

The animal makes a sound
The pig says: wee wee
The dog says: bow wow

7. Message passing:

Objects communicate with one another by sending and receiving information from each other. A message for an object is a request for execution of a procedure. Thus will invoke a function in the receiving object that generates the desired results. Message passing involved specifying the name of the object, the name of the function, and the information to be sent..

Recommended Post:

Event listener in java

Event handling in java

Convert java object into a JSON

Difference between the Java and JavaScript

Class method in javahttps://learnwithshikha.com/class-method-in-java/

Reference:

geeksforgeeks.org

w3school.com

Leave a Comment