In this article, you will read about Classes and Objects in Java, How to create classes and Objects in java with programs and examples.
Need of Classes and Objects in Java
There are many available data types in Java such as int, char, float, etc. These data types are used to store different types of variables in java. For example, if we need to store the age of a person, then we have to make a data type of int. But sometimes there is a need to store a set of values that belong to different types. For example, we have to store information about a student like his name, roll number, age, etc. So, we need to make multiple variables with different data types. But similarly, there can be many students and making so many different data types can be tedious, and also it will lead to name collision between variables.
So, to avoid this problem, Object Oriented programming provides us with the concept of classes. By the use of class, we can group several data types and make a user-defined or non-primitive data type. For example, we will define a student class, that will group the data types representing age, name, and other information related to a student.
What is a Class in Java
- Class is a way to group all the properties related to an object.
- Creating a class is similar to creating a data type.
What are Objects in Java
- The object is an instance of a class.
- It is a real-world entity and consumes memory in a program.
- Creating a class will not consume memory until it is instantiated, which means its object is created.
Creating Class in Java
- The class keyword is used to define a class followed by the name that we want to give to our class.
- After that, inside the body of the class, we can write the variables that we want to use under the class.
- Also, there are functions inside a class representing the properties of the class. These functions are also called methods
- We can refer to the example below to understand this much more clearly –
Class Box{ private int length; private int breadth; private int height; public void show(){ System.out.prinln(“length is” + length); System.out.println(“breadth is” + bredth); System.out.println(“height is” + height);} }
Creating Objects in Java
Box mybox = new Box();
By writing this syntax a reference variable name ‘mybox’ is created that is pointing to the Box that we have created using Class. We can access the class using the object.
Setting visibility of Classes
The visibility of classes can be set using the access Modifiers. Also, the methods and variable’s accessibility can also be changed by using these access modifiers.
The access modifiers that we can use to control the accessibility of class are –
Default – Declarations are visible within the package only
Private – Declarations are visible within the class only
Protected – Declarations are visible within the package or all subclasses
Public – Declarations are visible everywhere.
We will now see a working program to see the uses of classes –
Class Box{ private int length; private int breadth; private int height; public void set(int l, int b, int h){ length = l; breadth = b; height = h;} public void show(){ System.out.println(“The length is” + length); System.out.println(“The breadth is” + breadth); System.out.println(“The height is” + height);} } class Example{ public static void main(){ Box mybox = new Box(); mybox.set(8,10,12); mybox.show(); }}
In the above code, a box named non-primitive data type is created. Then, inside it, variables are created that will be used to hold the length, breadth, and height of the box respectively. Also, there are methods defined named set and show that are used to set the values to these variables and print the values to the console screen respectively.
Inside the main class, the object of the box is created after which the class we defined comes to memory. Mybox named variable of this class is created which is pointing to the box and will be used to access the variables and methods of the class.
Output –
The length is 8 The breadth is 10 The height is 12.
In this article, we have covered the classes and objects in detail and also the access modifiers used to control the visibility of the class.
Related Quiz: