Learning Path Java — Part 2 (Classes, Object, Constructor, Visibility Modifiers)

Balaji S B
Proggy Blast
Published in
3 min readAug 12, 2022

Hello everyone, welcome back to Proggy Blast. In part -1 we mentioned, what are the basic topics we need to learn for java development. In this article we are going to study the what is classes, objects, constructor, visibility modifier. Here we go.

Class :

Class is generally defined as the collection or group of variables and methods into single entity.

Example:

class ProggyBlast {

private static String name = “Proggy Blast”;

public static void main(String args[]) {

System.out.println(“Welcome to ”+ name);

}

}

In the above example, class is the reserved keyword and ProggyBlast is the class name. Name is declared as static string and main method is the function declared as static. For the detail about the static declaration we will discuss later.

Object :

Object is generally referred as the instance of the class. It is actually pointed towards the memory location. Using the instance we can access the methods and variables of the particular class. Based on previous class example, we can see how to create the object.

Example :

class ProggyBlast {

private static String name = “Proggy Blast”;

public static void main(String args[]) {

System.out.println(“Welcome to ”+ name);

}

}

class Hello {

ProggyBlast pb = new ProggyBlast();

}

In the above example ProggyBlast is the class and pb is the instance or object of the class. Here with the help of pb we can access the public methods and public variables. In this line “new” is the keyword and constructor of the class, which is used to create the object. Whenever we create the object it allocates address and point towards in the heap memory.

Constructor :

Constructor is defined as the function where the function name is same as class name and it doesn’t have the return type. The return type of the constructor is generally void.

With the help of constructor we can create the object.

We have 2 types of constructor

1. Default Constructor

2. Parameterised Constructor

Example :

class ProggyBlast {

private static String name = “Proggy Blast”;

ProggyBlast() { // default constructor

System.out.println(“Hello World from default Constructor”);

}

ProggyBlast(String name) { // parameterized constructor

System.out.println(“Hello ”+ name + “from parameterized constructor”);

}

}

Visibility Modifier :

Visibility modifier is the keyword where we can declare the visibility of variable or function. There are 4 types of visibility modifiers.

1. default

2. private

3. protected

4. public

Based on the usage we can use the visibility modifier.

1. default :

If we are not declare anything for the function or variable declaration it should be considered as default.

Example :

int a =10;

In the above example the visibility modifier is not mentioned, so it is considered as default. And we can access the value of “a” from any class with the help of object within the same package.

2. private :

If we want to use the variable or function within the same class means we are using private keyword. If the variable or function is declared as private any class from outside can’t able to call even with the help of objects.

Example :

private int a = 10;

So the value of ‘a’ is accessable within the same class.

3. protected :

If we want to use or modify the particular variable or function from the outside of the class without the help of object we can use the keyword protected.

If we declare the variable or function with protected keyword, whoever is extending this class, that class only can use or modify the value. In general this variable declaration is achieved within inheritance.

Example :

protected int a=10;

4. pubic :

Without any restriction we want to use or update the value or function from any package we can use the public keyword.

Example :

public int a = 10;

Summary :
This article covers the basics of classes, object, constructor and visibility modifiers in details. Please do clap (👏) if you like this post. Thanks and Cheers.

Follow us on Instagram, Twitter, Facebook, LinkedIn.

#proggyblast #java #classandobject #balajisb Balaji S B Proggy Blast

--

--

Balaji S B
Proggy Blast

Technical Lead @ Happeist Minds Technologies Pvt Ltd