Classes

Java OOPs Features

Object Oriented Programming is a programming concept which involves creating classes and objects that perform the action upon it.

Classes And Objects

A class is a user defined structure or blueprint through which objects are created. It represents the set of properties or methods that are common to all the objects.

Object

An entity or unit of object oriented programming that has its own state and behaviours. It belongs to the things found in the real world.

Object declaration

Object creation is also known as object instantiation. All the instance share the attributes and behaviours of the class.

Example:

ClassName ObjectName;

If we declare in this manner, its value will be null untill an object is actually created and assigned to it. Declaring an object does not mean to create the object.

Object Initialization

Initialization of an object means to allocate memory for a new object and returning a reference to that memory.

Lets see an example:

public class Student {

	// Instance variable
	int rollNo;
	String name;


	// Constructor
	public Student(int rollNo, String name) {
		this.rollNo = rollNo;
		this.name = name;
	}


	void show() {
		System.out.println("Roll No: " + rollNo);
		System.out.println("Name: " + name);

	}

	public static void main(String args[]) {
		Student s1 = new Student(1, "Henry");
		s1.show();
	}

}
Output:

Roll No: 1

Name: Henry

Note: If a class does not contain constructor explicitly, the Java compiler automatically provide a no-argument constructor which is called as default constructor. This default constructor calls the parent’s no argument constructor or the Object class constructor. If the class has no other parent then Object class is present of all the classes directly or indirectly.

Various ways to create object

There are multiple ways in Java to create objects.

1. Using new keyword

It is the most common way to create an object in Java. Lets see an example:

public class Student {
	
	// Instance variable
	String name = "Huge Learning";

	public static void main(String args[]) {

		// Creating object using new keyword
		Student s1 = new Student();
		System.out.println(s1.name);
	}
}
Output:

Huge Learning

2. Using newInstance() method of class Class

java.lang.Class is a predefined class in Java. The forName() method returns the class object associated with the given class name. We need to give the fully qualified name of the class. With the help of newInstance() method on the class Class, object returns new instance of the class with the given string name.

Lets see an example:

package Tutorials;

import java.lang.Class;

class Test {

	void message() {
		System.out.println("Huge Learning");
	}

	public static void main(String args[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		Test t1 = (Test) Class.forName("Tutorials.Test").newInstance();
		t1.message();
	}
}
Output:

Huge Learning

3. Using clone() method

Clone method is also used to create Java object, when we call clone method in our program, JVM creates a new object and copies all content of previous object into it. JVM does not use or invoke constructor while creating object through clone method.

Note: To use clone() method on an object we need to implement Cloneable interface and define the clone() method in it.

Lets see an example

public class CloneTest implements Cloneable {

	public void display() {
		System.out.println("Huge Learning");
	}


	// Overriding clone() method of Object class
	public Object clone() throws CloneNotSupportedException {
		return super.clone();
	}


	public static void main(String[] args) {
		
		// Create a new object
		CloneTest ct1 = new CloneTest();
		CloneTest ct2;
		
		try {
			ct2 = (CloneTest) ct1.clone();
			ct2.display();
		} 
		catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
}
Output:

Huge Learning

Note: Here class need to implement cloneable interface otherwise it will throw CloneNotSupportedException.

4. Using Deserializatrion