Software Training Institute in Chennai with 100% Placements – SLA Institute

Easy way to IT Job

Share on your Social Media

Object-oriented programming (OOP) concepts in Java

Published On: April 1, 2023

SLA Institute is a leading training institute in Chennai that offers comprehensive Java training programs for beginners and experienced professionals. Their Java training in Chennai covers OOP concepts in-depth, helping students to develop a strong foundation in Java programming.

OOP is an abbreviation for Object-Oriented Programming. Procedural programming is all about writing methods or processes that execute actions on data, whereas object-oriented programming is all about constructing objects that include both data and methods.

Object-oriented programming has many advantages over procedural programming, including the following:

  • OOP is faster and easier to implement.
  • OOP gives the programs a clear structure.
  • OOP makes Java code easier to maintain, alter, and debug by encouraging it to be DRY (Don’t Repeat Yourself).
  • OOP allows you to create fully reusable applications with less code and in less time.

The following are some OOPS concepts:

  • Class
  • Object 
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Classes and Objects in OOPs

The two main components of object-oriented programming are classes and objects. If the class is “Color,” the objects are “Red,” “Green,” and “Blue.” If the class is a bike, the objects are RE, Yamaha, Duke, and Jawa. 

A class is a user-defined template or prototype that is used to create objects. It represents the collection of attributes or methods shared by all objects of the same type. Instead of writing their code multiple times, you can use classes to create multiple objects with the same behavior. This includes classes for objects that appear multiple times in your code. The major components of a class include the modifier, class name, superclass, interfaces, and body.

In object-oriented programming, an object is a fundamental building block that represents real-world objects. A typical Java program generates a large number of objects, which interact by invoking methods. The objects are the components of your code that are visible to the viewer or user. An object includes a state, behavior, identity, and method.

Example

public class SLA{

    static String Employee_name;

    static float Employee_salary;

    static void set(String n, float p) {

        Employee_name = n;

        Employee_salary = p;

    }

    static void get() {

        System.out.println(“Employee name is: ” +Employee_name );

        System.out.println(“Employee CTC is: ” + Employee_salary);

    }

    public static void main(String args[]) {

        SLA.set(“Hariharan”, 200000.0f);

        SLA.get();

    }

}

Output

Employee name is: Hariharan

Employee CTC is: 200000.0

Abstraction

Data abstraction allows the user to only see the most important information. The user doesn’t observe any extraneous or pointless units. For instance, a car is seen as a whole rather than as its distinct parts. Another definition of “data abstraction” is the process of identifying and removing all but the most essential components of an item. An object can be classified or grouped according to its characteristics and behaviors, which also help to distinguish it from other objects of the same sort.

Example

//abstract class

abstract class SLA{

  //abstract methods declaration

  abstract void add();

  abstract void mul();

  abstract void div();

}

Encapsulation

It is described as the collection of data into a single entity. It is the mechanism that connects the code to the data that it works with. Encapsulation can be thought of as a wall that prevents programming from the outside from accessing the data.

  • Strictly speaking, encapsulation means that a class’s variables or data are kept secret from other classes and are only accessible through member functions of the class in which they are stated. 
  • Encapsulation conceals a class’s data from other classes in a manner akin to data hiding. Encapsulation and data-hiding are interchangeable terms in this context.
  • Encapsulation is accomplished by making all of a class’s variables private and by including public methods to set and retrieve the variables’ values.

Example

Encapsulation using private modifier   

class Employee {

    private int empid;

      private String ename;

}

Inheritance

Inheritance is a crucial OOP concept (Object Oriented Programming). A class can adopt the characteristics (fields and methods) of another class using Java’s methodology. By employing the extends keyword, we can achieve inheritance. Another name for inheritance is an “is-a” relationship.

Let’s go over some key terms that are used frequently:

Superclass: A superclass is a class from which features are inherited (also known as base or parent class).

Subclass: Those classes that inherit from other classes are known as subclasses (also known as derived or child class). In addition to the attributes and methods of the superclass, the subclass may also add additional fields and methods.

Reusability: Inheritance supports the concept of “reusability”; for instance, if we want to create a new class but an existing class already has some of the code we require, we can get our new class from the previous class. By doing this, we are making use of the pre-existing class’s fields and methods.

Example

//base class or parent class or superclass

class A{

  //parent class methods

  void method1(){}

  void method2(){}

}

class B extends A{ //Inherits parent class methods

  //child class methods

  void method3(){}

  void method4(){}

}

Polymorphism

It speaks to the effective distinction between entities with the same name that object-oriented programming languages are capable of making. Java accomplishes this with the aid of these entities’ signatures and declarations. Polymorphism is the capacity to manifest in a variety of forms.

Example

// Java program to Demonstrate Polymorphism

// This class will contain

// 3 methods with the same name,

// yet the program will

// compile & run successfully

public class Sum {

    // Overloaded sum().

    public int sum(int x, int y)

    {

        return (x + y);

    }

    public int sum(int x, int y, int z)

    {

        return (x + y + z);

    }

    // Overloaded sum().

    public double sum(double x, double y)

    {

        return (x + y);

    }

    // Driver code

    public static void main(String args[])

    {

        Sum s = new Sum();

        System.out.println(s.sum(10, 20));

        System.out.println(s.sum(10, 20, 30));

        System.out.println(s.sum(10.5, 20.5));

    }

}

Output

30

60

31.0

Bottom Line

One can speed up development and increase code reuse by using the OOPs methodology. Between classes and objects, communication is established using simple message passing. Data abstraction and hiding are two characteristics that OOPs use to guarantee code security. You can acquire practical knowledge while obtaining an IBM-approved certification at SLA, the leading Java Training Institute in Chennai.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.