Class and Object in Java with Example

The class is the core of Java. It is the logical entity upon which the entire Java language is built because it defines the shape and nature of an object. As such the class is an important part of object-oriented programming in Java. Any concept you want to implement in a Java program must be encapsulated within a class. let's understand the concept behind classes and Objects in Java.

What is a Class?

The most important thing to understand about a class is that it is used to defines a new data type. Once we defined, this new data type can be used to create objects of that type. Thus, we can say that a class is a template for an object, and an object is an instance of a class. A class in Java contains methods, fields, constructors, blocks, and interfaces.

How to create a class?

We can create a class by use of the class keyword. A general form of a class definition is shown here:

class classname{
type instance-variable1;
type instance-variable2;
type instance variable n;

type methodname1(parameter-list){
//body of method

type methodname2(parameter-list){
//body of method

type methodnameN(parameter-list){
//body of method

The data or variables defined inside the class are called instance variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class and the Variables defined within a class are called instance variables  Thus, the data for an object is separate and unique from the data for another.

What is Object?

The Object is an entity that has state and behavior. The object is an instance of a class and an object is a real-world entity. To access the members which are defined in the class you need to create an object.
for example: a chair, pen, table, bike, book, etc are the example of Object.

How to create an object?

We can create an object with the help of a new operator. The new operator is used to dynamically allocates memory for an object and returns a reference to it.
for example:           Box obj = new Box();

Here we can create an object of Box class and obj is a reference to an object of type Box.
Class and Object in Java

Example using class and Object in Java:

In this example, we create a class called Box that defines three instance variables: width, height, and depth. Currently, Box does not contain any methods. In this example, we compute the volume of a box.

class Box{
 //declare member variables
 double width;
 double height;
 double depth;
class BoxVolume{
 public static void main(String args[]){
  //creating an object of Box class
  Box obj = new Box();
  double vol;
  //assign values to obj instance variable
  obj.width = 10;
  obj.height = 20;
  obj.depth = 30;
  //compute volume of Box
  vol = obj.width * obj.height * obj.depth;
  //printing the volume of Box
  System.out.println("Volume of Box is: " +vol);

The following example computes the volume of a box which is 6000.0 in this case.

You may also enjoy these posts:
1. Abstract class in Java with Example
2. Java polymorphism with Example
3. Encapsulation in Java OOP with example
4. Inheritance in Java with example
5. What is the role of MVC Architecture in Java?

Class and Object in Java with Example Class and Object in Java with Example Reviewed by Prashant Srivastava on May 05, 2019 Rating: 5

No comments:

Powered by Blogger.