Objects (Declaration, Initialization, Creation)

Objects: Declaration, Initialization, and Creation

When an object of a class is created, it is said to be instantiated. All the instances of a class share the attributes and behavior of the class. However, the values of those attributes, i.e., the state, are unique for each object. A single class may have any number of instances.

Declaring Objects

To declare an object in Java, we use the following syntax:

java
ClassName objectName;

This informs the compiler that we will use the name to refer to data whose type is ClassName. With a primitive variable, this declaration also reserves the proper amount of memory for the variable. For reference variables, the type must be strictly a concrete class name. In general, we cannot create objects of an abstract class or an interface.

Initializing an Object

The new operator is used to instantiate a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the class constructor.

In Java, a constructor is a method that has the same name as the class and has no return type. The Java compiler differentiates the constructors based on the number and type of the arguments.

Example:

java
public class Dog { // Instance Variables String name; String breed; int age; String color; // Constructor Declaration of Class public Dog(String name, String breed, int age, String color) { this.name = name; this.breed = breed; this.age = age; this.color = color; } // Other methods and variables of the class public static void main(String[] args) { Dog tuffy = new Dog("Tuffy", "Papillon", 5, "white"); System.out.println(tuffy.name); } }

The result of executing this code is Tuffy.

Ways to Create Objects

There are four ways to create objects in Java:

  1. Using new keyword: This is the most common and general way to create an object in Java.
java
ClassName objectName = new ClassName();
  1. Using Class.forName(String className) method: This method returns the Class object associated with the class with the given string name.
java
ClassName obj = (ClassName) Class.forName("packageName.ClassName").newInstance();
  1. Using clone() method: This method creates and returns a copy of the object.
java
ClassName objectName1 = new ClassName(); ClassName objectName2 = (ClassName) objectName1.clone();
  1. Deserialization: Deserialization is a technique of reading an object from the saved state in a file. Refer to Serialization/Deserialization in Java.

Creating Multiple Objects by One Type Only:

In real-time, we need different objects of a class in different methods. Creating a number of references for storing them is not a good practice, and therefore, we declare a static reference variable and use it whenever required. In this case, the wastage of memory is less. The objects that are not referenced anymore will be destroyed by the garbage collector of Java. For example:

scss
Test test = new Test(); test = new Test();

In the inheritance system, we use a parent class reference variable to store a subclass object. In this case, we can switch into different subclass objects using the same reference variable. For example:

java
class Animal {} class Dog extends Animal {} class Cat extends Animal {} Animal animal = new Dog(); // Storing subclass object

When we create an object of the Dog class and assign it to a variable of type Animal, we are essentially creating a reference to the Dog object with an Animal reference type. This means that the Animal variable can access only those members of the Dog class that are defined in the Animal class or its parent classes.

For example, suppose we have a method makeSound() in the Animal class that all animals must implement, but the Dog class also has a method bark(). If we call animal.makeSound(), it will call the makeSound() method implemented by the Dog class (assuming it overrides the makeSound() method in Animal). However, if we try to call animal.bark(), we will get a compile-time error because the Animal class does not define the bark() method.

In summary, when we create an object of a subclass and assign it to a variable of the parent class, we can access only those members of the subclass that are defined in the parent class or its parent classes, and not the members that are specific to the subclass.



Previous Post Next Post