Classes are template or blueprints from which objects are created. All the codes are written inside class. A method consists of state of the object(i.e, instance variables) and methods, which acts on those states. Classes provides encapsulation to data, as we define instance variables of class as private and provide public methods to act on those variables.

Classes and Objects in java

A java class will have instance fields and methods. While creating a class usually we choose nouns of our requirement as class names and verbs as methods (This is not mandatory, but this is a good practice). Example: Account is used as class name and createAccount as method name.

A simplest form of Java class will have following structure:

class Myclass {
 field1
 field2
 ......
 constructor1
 constructor2
 ......
 method1
 method2
 ......
}

Constructors are the methods which are called while creating a object of a particular class. Constructors are used for initialization purpose. A class can have more than one constructor, with different method signatures.

A class declaration contains following components:

  • Modifiers: public or default.
  • Class Name: Name of the class. Name of the class should begin with initial letter capitalized by convention.
  • Super Class Details: Name of parent class with extends keyword prefix. A class can extend only one super class.
  • Interface Details: Comma separated interface names, which are implemented by the class with implements keyword prefix.
  • Body: Class body with required instance fields, constructors and methods.

 

Objects

Objects are the actual entities, which uses class template. Objects contains states(attributes), behaviors(methods) and an identity (name of the object). Object is an instance of a class.

Creating Objects

We can create Objects in following different ways:

  • Using new key word : We declare object using class name, followed by name of the object. We use new keyword to instantiate objects. Finally, we use constructor to initialize the object. Following is the statement and example for creating and initializing object. new operator allocates memory to the object and returns reference to that memory.
ClassName objectName = new ClassName();
///Example:
Car car = new Car();
  • Using Class.forName()Here, we pass the Java class name for the forName() method and use newInstance() method to get the object. Following is the example. In this case constructor is called internally while creating the object.
Car car = (Car) Class.forName("com.asb.Car").newInstance(); 
or
Car car = Car.class.newInstance();
  • Using clone() method : By using the clone() method, we can create a copy of existing object. In this method, constructor is not called during the object creation. While using this method on Custom classes, make sure that the class implements Cloneable interface.
Car car1 = new Car();
Car car2 = (Car) car1.clone();
  • Using Deserialization : Creating object from sequence of bytes of an input stream(deserialization). Constructor is not called in this way of creation of object.
ObjectInputStream inputStream = new ObjectInputStream("asb.txt" );
Car car = (Car) inputStream.readObject();

Also Read: