Before discussing the OOPs concepts let us have a brief look at objects and classes with real-world examples.
Object in the Real world:
Any real-world entity is known as an Object. Every real-world object/entity has two characteristics state and behavior. Let us take the example of a car. Its state is defined by color, current speed, etc. and behavior is defined by applying breaks, changing speed, etc.
Object in programming:
Like every real-world object, software objects also have states and behaviors. The state of the object is represented by data members or fields and behavior is represented by methods.
Class in the Real world
Let us take the example of cars. There are thousands of cars in existence but all are built from the same set of blueprints and therefore contain the same components. In other words, your car is an instance (object) and cars are the blueprint (class of objects).
Class in programming:
Class acts as a blueprint or template for creating objects. It provides state and behavior for its objects. Java is a pure object-oriented language means everything we discuss in Java is an object.
Syntax:
access_modifier class class_name{ //Class Body }
Java class has mainly two types of access levels:
- Default: If a class is created with a default access specifier then the class objects are accessible only inside the package.
- Public: If a class is created with a public access specifier then the class objects are accessible in code in any package.
Note: If no access modifier is used it is taken as default.
Example:
package com.w3schools; public class Car { String carColor; int carSpeed; void addCarDetails(String color, int speed){ carColor = color; carSpeed = speed; } void displayCarDetails(){ System.out.println("Color: " + carColor); System.out.println("Speed: " + carSpeed); } public static void main(String args[]){ //Creating Car Objects Car car1 = new Car(); Car car2 = new Car(); //Need object here because method is non-static. car1.addCarDetails("Blue", 180); car2.addCarDetails("White", 130); System.out.println("Car1 Details: "); car1.displayCarDetails(); System.out.println("Car2 Details: "); car2.displayCarDetails(); } }
Output:
Car1 Details: Color: Blue Speed: 180 Car2 Details: Color: White Speed: 130
How to initialize an object in Java?
You can initialize objects in Java by using below 3 different ways.
- By Reference variable
Car car = new Car();
- By Method
public class TestClass { int value; public void initializeObject(int initialValue) { this.value = initialValue; } }
TestClass obj = new TestClass(); obj.initializeObject(10);
- By Constructor
public class TestClass { int value; // Constructor with parameter public TestClass(int initialValue) { this.value = initialValue; } }
TestClass obj = new TestClass(40);
Different ways to create an object in Java?
- By using a new keyword
TestClass obj = new TestClass();
- By using newInstance() method
try { Class<?> myClass = TestClass.class; // Create an object of TestClass TestClass obj = (TestClass) myClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { // Handle exceptions if there is any e.printStackTrace(); }
By using clone() method
TestClass originalObj = new TestClass(); TestClass copyObj = (TestClass) originalObj.clone();
- By using deserialization
FileInputStream fileInputStream = new FileInputStream("object.ser"); ObjectInputStream in = new ObjectInputStream(fileInputStream); // Creating an object from serialized data TestClass obj = (TestClass) in.readObject();
- By using a factory method
public class TestClass { public static TestClass createObject() { return new TestClass(); } }
TestClass obj = TestClass.createObject();