Object-Oriented Programming Concepts JAVA-Part 1

Mrigank Singh
CodeX
Published in
3 min readMar 17, 2022

What is a class?

Class is the blueprint from which objects are created. For example, there are two dogs one is a black lebra dog, and the other is a golden retriever. Both can be considered as real-world objects created from single blueprint dogs.

What are Objects?

Objects in programming are similar to real-world objects. The objects have a state which they store in the form of variables and behavior in the form of methods to interact with the state. Hiding the internal state and performing all interactions through object methods is known as data encapsulation. Benefits of building code with the help of objects:

  • Modularity → The source code for an object can be written and maintained independently of the source code for other objects.
  • Information Hiding → By interacting only with an object’s methods, its internal implementation details remain hidden from the outside world.
  • Code re-use → If an object already exists, you can use that object in your program.

Access Specifiers in JAVA

There are four access specifiers in JAVA which, in decreasing order of strictness, are:

  1. private: The methods and data members declared private are only accessible within the class.
  2. default: If we do not specify any access specifier with our methods or data variable, they are considered default access modifiers. The default members can be accessed only within the same package.
  3. protected: The protected data members and methods can be accessed only within the same package, and the class that inherits the class using protected members and methods.
  4. public: They can be used anywhere in any class and in any package.

By convention, we should use the most strict access modifier to perform sanity checks on our variable and control who can modify it.

This Keyword

When we call methods of a class using different objects of the same class, how do the methods know from which object call is coming? Behind the scenes, the reference to the object is passed to the function in this keyword internally.

Final Keyword

The final keyword is like the const keyword in other languages. This keyword can be used with the class, methods, and variables providing different use-case for each one.

Final keyword with variables

There are three types of variables local, class, and instance. Let’set’s see the use case of the final with each one of them.

  • Local variable → The final local variable cannot be re-initialized; otherwise, the compiler will throw an error.
  • Instance variable → The final instance variable will have to be initialized by using the initializer block, constructor, or at the time of declaring, and then it cannot be re-initialized. If it is not initialized for the first time using any of the above three methods compiler will show an error. For every instance of the class, there will be a final instance variable.
  • Class variable → The variable declared inside the class with both keywords final and static is a class variable. It remains the same for every instance of the class. It is like a global variable in other languages like C++ and C. The final class variable value remains the same for every class instance, and it cannot be re-initialized. As the final instance variable, it is necessary to initialize it during its declaration or in-class initializer block.

How to initialize a final variable in the initializer block in the case of the instance variable and the class initializer block in the case of the class variable?

This concept might be known to many readers, but as it was a new concept for me, I will put an example code for readers who don’t know about them.

Initialization blocks are executed whenever the class is initialized and before constructors are invoked.

Initializer Block

Class Initializer blocks are static initializer blocks.

Class Initializer Blocks

--

--