Object Oriented Programming (OOPs) Concept in Java

Like the name implies, Object-Oriented Programming is a term used to describe languages that make use of objects to program, they employ objects as a base for implementing what happens within the code. They are viewed by the user or viewer and perform tasks that are assigned by the user. Object-oriented programming is a way to incorporate real-world concepts such as inheritance, hiding polymorphism and more. in programming. The primary goal for OOP is to link the data and functions that work on them, so that no other element of the code is able to access this data unless that function.

Let’s discuss the the basics of method declaration and passing. Starting with the method declaration it comprises six elements:

  • Access Modifier The definition of the Access type of the of the method i.e. of how it will be of the method, i.e. from where it can be accessed by the of the method, i.e. from where it can be accessed in your. In Java there are four kinds of access specifiers:
    • publicly available: Accessible in all classes of your application.
    • secured: Accessible within the package within which it is defined, and also within the package’s subclass(es) (including the subclasses defined outside of this package).
    • Private: Only accessible within the classes for that it’s defined.
    • standard (declared/defined without an modifier): Accessible within the same package in which its classification is specified.
  • The type of return is the kind of result returned from the technique, or null if it fails to provide any value.
  • Method Name The rules for field names also apply to method names too however, the convention is slightly different.
  • List of parameters List of commas separated lists of input parameters that are identified, followed by their type of data, in parentheses. If there is no parameter then you need to make use of the empty brackets ().
  • List of exceptions: The exceptions that you anticipate to throw by the method. You can indicate the exception(s).
  • Method Body The body is the code block, contained between braces, which you have to execute in order in order to accomplish your purpose.

message passing : Objects exchange messages through the sending and reception of information to one another. A message to an object is a request to the execution of a process which is why it invokes an operation in the receiving object which produces the desired outcomes. The message passing process involves specifying what the object’s name is and the function’s name, as well as the information that needs to be transmitted.

After we have learned the fundamental prerequisites We will now move to the four OOPs’ pillars that can be described as follows. Let’s begin by understanding the various features of an Object-Oriented Language.

The OOPS concepts are in the following order:

  • Class
  • Object
  • Method and method passing
  • Pillars of OOPs
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
      • Polymorphism in Compile-Time
      • Polymorphism in the runtime

The term “class” refers to class is an user-defined blueprint or prototype that is used to design objects are constructed. It is the collection of methods or properties that are shared by every object of a particular type. With classes, you can create multiple objects that have similar behavior without writing the code over and over. This can be done for objects appearing more than one time within your program. In general, classes declarations could contain these elements in order:

  1. Modifiers The class can be made public as well as have default access (Refer to this article
  2. The name of class Class name must begin with the initial letter capitalized in accordance with convention.
  3. Superclass (if there is one): The name of the class’s parent (superclass) If any preceded by the word extends. A class is only able to expand (subclass) only one of its parents.
  4. Interfaces (if there are any): A comma-separated list of interfaces used within the class in the event of any followed by the term implements. A class may implement multiple interfaces.
  5. Body Body of the class is enclosed by braces.

A objectis the most basic element of Object-Oriented programming that is a representation of real-life objects. An average Java program creates numerous objects that, like you, interact through methods. The objects execute your code. They constitute the component of your program that is visible to the user or viewer. A typical object is composed of:

  1. State It is represented through objects’ attributes. It also represents the characteristics of the object.
  2. Behavior The behavior is depicted by the mechanisms that an object uses. It also shows the reaction of the object to other objects.
  3. Identity is a distinct name given to an object that allows it to communicate with objects.
  4. Method : A method is a sequence of commands that execute a specific task and then return the results to the user. Methods are able to perform a specific task , but without giving anything back. Methods let us recycle the code, without having to type it, which is the reason they are regarded as efficient in their use. In Java every method has to be part of a class, which differs from other languages such as C, C++ and Python.

Let’s discuss the 4 pillars that make up OOPs:

Pillar 1: Abstraction

The Data Abstraction refers to the fact that means of which only most important information is displayed for the users. Non-essential or irrelevant components aren’t displayed in front of the user. Example: A car is seen as a car , rather than the individual parts.
Data Abstraction could also be described to be the act of finding the necessary attributes of an object and not focusing on the details that are irrelevant. The characteristics and actions of an object are what differentiate them from objects that are of a similar kind and assist in classifying or grouping the object.
Think of a scenario that is real-world in an individual driving in a car. The driver only knows that pressing the accelerator will increase the car’s speed or stopping the vehicle with brakes. However, the man doesn’t know why when he presses the accelerator, the speed actually increases. He doesn’t know the internal mechanism of the car or about the use of the brakes, accelerators and so on. inside the car. It’s what abstraction is.
In Java abstracting is accomplished through [interfaces and abstract classes. It is possible to achieve 100% abstraction by using interfaces.

Pillar 2: Encapsulation

It’s the wrapping of data in one unit. The mechanism that holds both the code and information it processes. Another way to think of the concept of encapsulation is to think of it as the protection shield that blocks information from getting accessible by code that’s not inside this.

  • In encapsulation, the variables or the information inside a class are kept from other classes and is accessible only via each member function the class they were declared.
  • Encapsulation is when the data within the class is concealed by other classes and this is identical to the way hiding data does. Therefore the words “encapsulation” or “data-hiding” are interchangeable.
  • Encapsulation can be accomplished by declaring all variables of a class as private and then writing public methods within the class to determine and set the values of variables.

Pillar 3: Inheritance

Inheritance is a key pillar that is a key pillar of OOP (Object Oriented Programming). It is the process in Java where a class is permitted to take over the characteristics (fields or methods) of a different class.

Let’s look at some commonly employed terminologies of importance:

  • SuperclassThe classes whose characteristics are passed on is called superclass (also called base class or parent class).
  • Subclasses: The class that inherits another class is called a subclass (also known as derived , expanded or child classes). Subclasses can also add their own methods and fields in addition to superclass’s techniques and fields.
  • ReusabilityInheritance is a good example of “reusability”, i.e. when we wish to create the new class but there’s already an existing class that contains some of the code we would like, we can create a new class by utilizing that class. In this way by reusing some of the methods and field that are in the class already in use.

Pillar 4: Polymorphism

It is the capacity of programming languages that are object-oriented to distinguish between entities that have identical names efficiently. This is accomplished by Java by using their signatures and the declarations of the entities.