Class and Objects in Java

Class and Objects in Java

In this article, I am going to discuss Class and Objects in Java with some real-time examples. Please read our previous article, where we discussed the basic concept of Object-Oriented Programming in Java. At the end of this article, I am sure, you will understand what is a class and what is an object and how we created objects and classes in Java.

What is Object in Java?

The object is an instance of a class. Objects can be tangible or intangible. When you create a class, you are creating a new data type. You can use this type to declare objects of that type. You can do this using the new operator. The new operator dynamically allocates memory for an object and returns a reference to it. This reference is then stored in the variable.

From a programming point of view, an object can include a data structure, a variable, or a function. It has a memory location allocated. The object is designed as class hierarchies.

Creating an Object: new Keyword

The Java new keyword is used to create an instance of the class.  It instantiates a class by allocating memory for a new object and returning a reference to that memory. We can also use the new keyword to create the array object.

Syntax : ClassName ReferenceVariable = new ClassName();

Important Note:
  • It is used to create the object.
  • It allocates the memory at runtime.
  • All objects occupy memory in the heap area.
  • It invokes the object constructor.
  • It requires a single, postfix argument to call the constructor.
Sample Program from Creating Object using the new keyword:
package Demo;

public class NewDemo {
  void display()  
     {  
         System.out.println("Invoking Method");  
     } 
 public static void main(String[] args) {
  NewDemo obj=new NewDemo();  
        obj.display(); 

 }
}

Output: Invoking Method

Characteristics of Objects in Java:

An object has three characteristics :

  1. State: It refers to the basic data it represents. It represents the properties of an object and is represented by the instance variable/attribute of an object. The properties of an object are important because the outcome of functions depend on the properties.
  2. Behavior: It represents the functionality or actions of the object. It is represented by methods in Java.
  3. Identity: It is the unique identification of the object. It differentiates one object from the others.

Example: Let’s take a pencil. A pencil is an object. Its name is Natraj. State: the color is black. Behavior: It is used to write so writing is behavior.

Note:

  • Objects are run time entity. They are always getting a memory at run time.
  • Objects are created from a class and methods or actions are performed on objects.
What is Class in Java?

A class is a blueprint for the object. A class is declared by the use of the class keyword. The data or variables declared within a class are called instance variables because each instance of the class contains its own copy of these variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.

From a programming point of view, a class is basically user-defined data types that act as a template for creating objects of the identical type. It represents the common properties and actions (functions) of an object. The syntax to create a class in Java is given below.

class <class_name>{
      field;
     method;
}

Example: Consider you have iPhone, Samsung, and Sony devices and you want to represent them in JAVA. Here, all the phones have some properties like brand, color, weight, etc. But the type of all the phones is Mobile. And here blueprint can be a Mobile because they all are a type of Mobile. So Mobile is a class that can represent the iPhone, Samsung, and Sony devices here.

How to Declare class in Java?

A class can be declared using the keyword class followed by a class name. It has also a body within braces. The general syntax to declare a class is shown below. The syntax is given below. 
modifierName class className
{
// class body.
}

Example:

Class Mobile{
      /** to do code here */
}

Components of Class in Java:

In general, a class can have the following components to act as a template:

Modifiers:

In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors, data members, and setter methods. It can be public, private, protected, and default.

Class Name:

By convention, a class name should begin with a capital letter and subsequent characters lowercased (for example Employee). If a name consists of multiple words, the first letter of each word should be uppercased ( for example PermanentEmployee). A class name can also start with an underscore ”_”. The following class name can be valid such as _, _Employee. Keywords cannot be a valid class name.

Body:

Every class’s body is enclosed in a pair of left and right braces. In the body,  a class can contain the following members:

  1. Field declarations;
  2. Constructor declarations;
  3. Method declarations;
  4. Instance block declarations;
  5. Static block declarations;

Note:

  • Class is a representation of similar types of objects or it is an implementation of encapsulation.
  • The class itself consists of various methods and variables.
Difference Between Class and Object in Java:
CLASS OBJECT
A template for creating and instantiating objects within a program. An instance of a class.
Logical entity Physical entity
Declared with the “class” keyword. Created using the “new” keyword.
A class does not get any memory when it is created. Objects get memory when they are created.
A class is declared once. Multiple objects are created using a class.

Consider mobile and remodel it into a software entity. A mobile can have characteristics like color, model, battery backup, and can perform actions like playing games, clicking photos, messaging. So far, we have identified the following things:

Class Name: Mobile
Data Members: color, model, battery backup
Member Functions: playing games, clicking photos, messaging

Sample Program for Class and Object in Java:
package Demo;

public class ClassInJava {
    String brand;
    String model;
    int modelno;
    
    //Constructor

    ClassInJava(String brand,String model,int modelno){

        this.brand=brand;
        this.model=model;
        this.modelno=modelno;
    }
    public static void main(String[] args) {
        
        //Object of class ClassInJava
        ClassInJava object=new ClassInJava("iPhone","iphone SE", 6523);

        System.out.println("Mobile Brand is: " + object.brand);
        System.out.println("Mobile Model is: " + object.model);
        System.out.println("Mobile's Model No. is: "+ object.modelno);   
   }
}

Output:

What are Class and Objects in Java

In the next article, I am going to discuss Constructors in Java with examples. Here, in this article, I try to explain What are Class and Objects in Java as well as we also discussed how to create and use Class and Objects in Java. I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *