Simplifying Object Oriented Programming Concepts

Simplifying Object Oriented Programming Concepts

Java Classes

A class in Java is a set of objects which shares common characteristics and common attributes. It is a user-defined blueprint or prototype from which objects are created.

Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.

Class does not occupy memory. Class is a group of variables of different data types and a group of methods.

Java Objects

Objects are the instances of a class that are created to use the attributes and methods of a class.

When an object of a class is created, the class is said to be instantiated.

Object occupies some space in memory.

Properties Of Object

State: It is represented by attributes of an object.

Behavior: It is represented by the methods of an object.

Identity: It gives a unique name to an object.

Create Objects

We can create objects using the "new" keyword.

The new keyword dynamically allots memory at run-time and returns a reference to it.

We don't use "new" keyword with primitive datatypes as the primitive datatypes are not stored as objects in java. They are stored in the stack.

Manipulate Objects

Java Constructors

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

Every time an object is created using the new() keyword, at least one constructor is called.

A constructor that has no parameters is known as default the constructor.

"this" Keyword

The "this" keyword refers to the current object in a method or constructor. The "this" variable gets replaced with the reference variable.

Constructor Overloading

The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.

Constructor Chaining

Constructor chaining within the same class is done using the this() keyword.

Wrapper Class

A Wrapper class in Java is a class whose object wraps or contains primitive data types. They convert primitive data types into objects.

Garbage Collection

In C/C++, a programmer is responsible for both the creation and destruction of objects.

But in Java, the programmer need not care for all those objects which are no longer in use. Garbage collector destroys these objects. The main objective of Garbage Collector is to free heap memory by destroying unreachable objects.

The finalize() method is called by Garbage Collector.

Did you find this article valuable?

Support Reuben's blog by becoming a sponsor. Any amount is appreciated!