23 May Java Classes and Objects
What is a class in Java?
Classes are a construct on which the entire Core Java is built. A class is the basis of object-oriented programming in Java. In this tutorial, we will learn about Classes and Objects in Java. In Java, class is a template for an object, whereas an object is an instance of a class. Now, you would be thinking, why? Well, here’s the reason:
Class defines a new data type, which after defining can be used to create objects of that type. This is why, class is a template for an object and object an instance of class.
Class Definition
At first, to declare a class, use the class keyword in Java. The class is a reserved word in Java. Let us now see how to define a class in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class yourclassname { type instance-var1; type instance-var2; type instance-var3; // ... type instance-variableN; type methodname1(parameter-list) { // method body } type methodname2(parameter-list) { // method body } type methodname3(parameter-list) { // method body } // ... type methodnameN(parameter-list) { // method body } } |
In the above syntax, the <yourclassname> is the name of the class user needs to set. Before going further, let us first look into the terms used with class. The <instance-var1>, <instance-var1>, etc. are the instance variables. <methodname1>, <methodname2>¸etc are the name of the methods.
Instance Variables
Variables defined in a class are Instance Variables. Each instance (object) of the class has its own copy of these variables. Example is shown below.
Class Members
Variables and methods in a class are Class Members
What is an Object in Java?
An object is an instance of a class i.e. object is created from a class. Object represents real-life entities, for example, a Bike is an object. Object has State, Behavior, and Identity:
- Identity: Name of Bike
- State (Attributes) : The data of object Bike i.e. Color, Model, Weight, etc.
- Behavior (Methods): Behavior of object Bike like to Drive, Brake
Let us now see the representation, with Bike as an object:
Therefore, Java is all about classes and objects, with the attributes and methods.
Creating a class in Java
Let us see an example wherein we will create a class and understand Instance Variables and Class Members concept.
We have a class here, “Rectangle” with two instance variables:
1 2 3 4 5 6 |
class Rectangle { double length; double width; } |
Creating object in Java
An object is a runtime entity with state and behavior. Object also has identity, which isn’t revealed to the user. To create an object in a class, use the new operator. Let us see the above example and create an object rct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Rectangle { double length; double width; } public class StudyopediaDemo { public static void main(String args[]) { Rectangle rct = new Rectangle(); double area; rct.length = 10; rct.width = 5; System.out.println("Length of Rectangle = "+rct.length ); System.out.println("Width of Rectangle = "+rct.width ); area = rct.length * rct.width; System.out.println("Area of Rectangle = "+area); } } |
The output is as follows:
1 2 3 4 5 |
Length of Rectangle = 10.0 Width of Rectangle = 5.0 Area of Rectangle = 50.0 |
Above, we have created an object rct using new operator. This dynamically allocates memory for an object:
1 2 3 |
Rectangle rct = new Rectangle(); |
Let’s see the below representation. We have object rct:
The rct above is now an instance of Rectangle. Now, every Rectangle object will have its own copy of the instance variables length and width. We used the dot operator above to access the instance variables:
1 2 3 4 |
rct.length = 10; rct.width = 5; |
Above, the copy of instance variables length and width contained in the object rct are assigned values using the dot operator.
Creating multiple objects
We saw above how to create an object and its relation with instance variables in a class. With Java, we can create multiple objects in the same class. Let us see an example demonstrating multiple objects and showing each instance (object) of the class has its own copy of instance variables:
The code is as follows to create multiple objects in a single class; each object having its own copy of instance variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Rectangle { double length; double width; } public class StudyopediaDemo { public static void main(String args[]) { // creating 2 objects Rectangle rct1 = new Rectangle(); Rectangle rct2 = new Rectangle(); double area; // object rct1 with its own copy of instance variables rct1.length = 10; rct1.width = 5; System.out.println("Length of Rectangle1 = "+rct1.length ); System.out.println("Width of Rectangle1 = "+rct1.width ); // object rct2 with its own copy of instance variables rct2.length = 20; rct2.width = 15; System.out.println("Length of Rectangle2 = "+rct2.length ); System.out.println("Width of Rectangle2 = "+rct2.width ); } } |
The output is as follows:
1 2 3 4 5 6 |
Length of Rectangle1 = 10.0 Width of Rectangle1 = 5.0 Length of Rectangle2 = 20.0 Width of Rectangle2 = 15.0 |
Let us now see below, we have two objects rct1 and rct2. Each of these objects have their own copy of instance variables length and width:
Above, we have created two objects rct1 and rct2. Then, we assigned values to each object with its own copy of instance variables i.e. for rct1:
1 2 3 4 5 |
// object rct1 with its own copy of instance variables rct1.length = 10; rct1.width = 5; |
For object rct2:
1 2 3 4 5 |
// object rct2 with its own copy of instance variables rct2.length = 20; rct2.width = 15; |
Let us now see another example, wherein both rct1 and rct2 will refer to the same object. We will perform assignment on object reference variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
class Rectangle { double length; double width; } public class StudyopediaDemo { public static void main(String args[]) { // creating a single object Rectangle rct1 = new Rectangle(); double area; // object rct1 with its own copy of instance variables rct1.length = 10; rct1.width = 5; System.out.println("Length of Rectangle1 = "+rct1.length ); System.out.println("Width of Rectangle1 = "+rct1.width ); // assigning System.out.println("\nrct1 and rct2 will now refer to the same object...\n"); Rectangle rct2 = rct1; System.out.println("Length of Rectangle2 = "+rct2.length ); System.out.println("Width of Rectangle2 = "+rct2.width ); } } |
The output is as follows:
1 2 3 4 5 6 7 8 9 |
Length of Rectangle1 = 10.0 Width of Rectangle1 = 5.0 rct1 and rct2 will now refer to the same object... Length of Rectangle2 = 10.0 Width of Rectangle2 = 5.0 |
In the above code, at first, we created a single object:
1 2 3 |
Rectangle rct1 = new Rectangle(); |
After that, by assigning, we referred both to the same object:
1 2 3 |
Rectangle rct2 = rct1; |
Adding Methods
We will add a method to the class Rectangle created above. The instance variables will be accessed in this method and the area of rectangle will be calculated:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Rectangle { double length; double width; void displayArea() { System.out.println("Area of Rectangle = "); System.out.println(length * width); } } public class StudyopediaDemo { public static void main(String args[]) { Rectangle rct = new Rectangle(); rct.length = 10; rct.width = 5; System.out.println("Length of Rectangle = "+rct.length ); System.out.println("Width of Rectangle = "+rct.width ); rct.displayArea(); } } |
The output is as follows:
1 2 3 4 5 6 |
Length of Rectangle = 10.0 Width of Rectangle = 5.0 Area of Rectangle = 50.0 |
In this tutorial, we saw how work with classes and objects in Java. We also saw how to create classes and object, and the concept of instance variables, class members, etc.
No Comments