Java OOPS (Object Oriented Programming)

Aug 31, 2023·

4 min read

Object Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts.


A class can be defined as a blueprint from which you can create an individual object. Class doesn't consume any space. It can also be defined as user-defined data types, we use it when we want to create a data type with multiple attributes of different types. It is a convention to capitalize the first letter of the class name.


Objects are instances of class. An object contains an address and takes up some space in memory.

Creating Classes and Their Objects

A class can be created using public static class <Class_Name>{attributes}

Their objects can be created using <Class_Name> <Object_Name> = new <Class_Name>

Generally, we create classes outside the main function because if we create the class inside the main function then all the functions outside the main function will not have access to it, they could not even know that a new user-defined data type has been created and thus we cannot pass this new data type to functions outside main.

  • Note: Classes are passed by reference.

Access Modifiers

Getter and Setter

Getter and setter functions, also known as accessor and mutator methods, are methods used to access and modify the private fields (variables) of a class in object-oriented programming languages.

Getter Method: A getter method is used to retrieve the value of a private field. It typically has a name that starts with "get" followed by the name of the property it retrieves. Getter methods have no arguments and return the value of the property.

Setter Method: A setter method is used to modify the value of a private field. It typically has a name that starts with "set" followed by the name of the property it sets. Setter methods accept an argument representing the new value and update the property with that value.

We can use the getter using s1.getRno(); and the setter using s1.setRno(76) where s1 is the object of class student.

'this' keyword

When a local variable in a method or constructor has the same name as an instance variable, we can use this to refer to the instance variable. This helps avoid confusion and ensures that the correct variable is being accessed.


Constructor is a special type of method that is used to initialize and create objects of a class. Constructors are automatically called when an object of the class is instantiated (created). They are responsible for setting initial values to the object's attributes.

Constructors have the same name as the class and do not have a return type, not even void. They are distinguished by their parameter lists.

Constructors are called automatically when an object is created using the new keyword.

If you don't provide any constructors in your class, Java provides a default constructor with no arguments. It initializes the instance variables to their default values (e.g., null for objects, 0 for numbers, false for booleans).

Like regular methods, constructors can be overloaded by defining multiple constructors with different parameter lists this is known as Constructor Overloading.

The first constructor is the same as the default constructor having no arguments.

The second constructor is the constructor having three arguments and it initializes all the values which are passed when the objects are being created.

'final' keyword

The final keyword is used to declare entities (variables, methods, and classes) as unchangeable or immutable. Once a variable, method, or class is declared as final, its value or behavior cannot be modified or overridden.

'static' keyword

We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class. If we declare any variable as static, it is known as a static variable it becomes the variable of the class rather than a variable of the object. The static variable can be used to refer to the common property of all objects (which is not unique for each object). The static variable gets memory only once in the class area at the time of class loading. It makes your program memory efficient (i.e., it saves memory).

static functions are used if we want to access a function of a class just by using the <className>.<functionName> without creating an object of the class.

Thanks for Reading

Keep Coding