Java Objects and Classes

Java objects and classes are fundamental features of programming. Java is object oriented programming OOP language. Each executable program in java has a class. The class further contains methods and attributes.

For example, in real life vehicle is a class and the types of vehicles like car, truck, bike etc. are objects of vehicle class. A class can define multiple objects.

Java Object

Java Object is an instance of a class and can be created from a class. Objects can have various properties like attributes and methods. These attributes and methods are defined in the class. For example, car is an object of class vehicle and car can have its own properties like color, number of tires and functions like acceleration and steering.

Each object has different behavior as compared to other objects of the same class. For example, apple, banana, strawberry all are objects of class fruit. We see many real life examples of objects and classes in our daily life.

Java Class

Class defines an object. It is the blue print that is needed to create an object. A class can have:

  • Class Name
  • Attributes (Variables)
  • Methods (Functions)
  • Class body
  • Object
  • Access Specifier
  • Constructors

The keyword class is used to define a class. The syntax for defining a class in Java is:

Here access_modifier is the access specifier of class. Access Modifiers can be private, public or protected. But for class we use Public access modifier. ClassName is the name of the class. Note that first letter of the class name should be in upper case. The curly braces {} contain the functionality (attributes and methods) of class. It is called the class body.

For example, the below code shows how to create a simple class that prints Hello World!.

public class MyProgram {        // Class Name
   public static void main(String []args) {
      System.out.println("Hello World!"); // prints Hello World!
   }
}

Here, the name of class is MyProgram so the name of this file should also be MyProgram. As in Java the name of .java file and the name of public class should be same. The output of the above program after compilation will be:

Create Java Class

Lets consider another example of a class where the class has variables and methods. Further we can create java objects from that class. The below code creates a class named Vehicle.

public class Vehicle {
   String type;
   int tires;
   String company;

   void accelerate() {
   }

   void stop() {
   }

   void steer() {
   }
}

In the above program, the class vehicle has attributes like type, tires, company of various data types. Similarly it has three methods accelerate(), stop() and steer().

Create Java Object

After defining a class, user can use the class to create multiple java objects from it. The keyword new is used to create an object of the specific class. The syntax for creating an object is:

Here, ClassName is the name of class and objName is the name of the object. An object is created inside the main() method.

Here is a simple code that creates a class and its object. The class contains a single variable.

public class Count {
  int n = 3;
  public static void main(String[] args) {
    Count obj = new Count();
    System.out.println(obj.n);
  }
}

The output of the above code after compilation is

Class Constructors

Constructors are an important part of a class. Each class must have at least one constructor. If programmer does not create a specific constructor for a class, then compiler creates default constructor. Constructors are called by default when an object is created. The name of the constructor should be same as that of class.

The below code demonstrates the concept of constructors:

public class Vehicle {
   public Vehicle(String type) {
      // This constructor has one parameter, type.
      System.out.println("Vehicle type is :" + type );
   }
   public static void main(String []args) {
      // This statement will create an object car
      Vehicle car = new Vehicle( "car" );
   }
}

The output of the above code after compilation is:

Clone an Object

Java allows programmer to create clone of a java object. The method clone() is used to create clone object. The syntax for creating clone of an object is:

Access Object Variable

Programmer can access the instance variable using the dot operator (.). The syntax to access variable of a specific object is:

Here objName is the name of object and VariableName is the name of variable.

Access Object Method

Programmer can access the instance method using the dot operator (.). The syntax to access method of a specific object is:

Here objName is the name of object and MethodName() is the name of method.

The below code creates a Vehicle class. The Vehicle class has price variable and an object obj. It shows how to access object variable and method.

public class Vehicle {
   int price;
   public Vehicle(String type) {

      // This constructor has one parameter, type.
      System.out.println("Vehicle type is :" + type );
   }
   public void setPrice( int p ) {
      price = p;
   }
   public int getPrice( ) {
      System.out.println("Vehicle's price is :" + price );
      return price;
   }
   public static void main(String []args) {
      // Object creation
      Vehicle obj = new Vehicle( "car" );

      // Calling instance method
      obj.setPrice( 200000 );

      // Calling instance method
      obj.getPrice( );

      // accessing instance variable
      System.out.println("Value of Class Variable is:" + obj.price );
   }
}

The output of the above code after compilation is: