DEV Community

Rajendra🙏🏼👇🏽
Rajendra🙏🏼👇🏽

Posted on

Base Java Part 2 (Class vs Object)

This is part of the Base Java series, in this part 2 we discuss

  1. Difference between Class & Object.
  2. How to create an object in Java.
  3. Talk about constructor in Java.
  4. Different ways to create an object in Java.

Here is the full video of the part 2 Base Java Series. Please show some love and subscribe to my channel Subscribe.

Class

A class is a template or a set of instructions to build a specific type of object, it determines how an object behaves and what the object contains.

Object

Objects represent real-life entities, Object determines the behavior of the class, the object has a state, behavior, and identity. Object implements the state and behavior in the form of variables and methods and requires some memory allocated.

Let's take the example, you want to build a car management system

You will define car class which will have a name, type, color, and functions like running, autopilot, stopping, etc.

The objects are tesla, accord, Lexus, etc.. as shown in the below image

Class Object

Different ways to create an Object

We create an object in Java in multiple ways

  1. "new" keyword.
  2. "newInstance()" method.
  3. using "Clone()" method.
  4. using "ClassLoader".

The below Car Class and SampleJavaProject Class demonstrates these 4 ways to create Objects in Java.

Car.java

package com.test.java;

public class Car implements Cloneable{

    private String Name;

    private String Type;

    private String Color;

    public Car() {
        super();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }


    public  Car(String name, String type, String color) {
        this.Name = name;
        this.Type = type;
        this.Color = color;
    }

    @Override
    public String toString() {
        return "Car [Name=" + Name + ", Type=" + Type + ", Color=" + Color + "]";
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public String getColor() {
        return Color;
    }

    public void setColor(String color) {
        Color = color;
    }


}

SampleJavaProject.java

package com.test.java;

public class SampleJavaProject {

    public static void main(String [] args) {

        Car accord = new Car("Accord","2WD","Blue");
        Car tesla = new Car("Tesla","4WD","White");
        Car camry = new Car("Camry","2WD","red");
        try {
        Class clsClass = Class.forName("com.test.java.Car");
        Car tesla2 = (Car)clsClass.newInstance();
        tesla2.setColor("grey");
        tesla2.setName("model3");
        tesla2.setType("AWD");
        System.out.println("tesla2---->"+tesla2.toString());
        }catch(Exception e) {
            e.printStackTrace();
        }
        try {
        Car obj1 = new Car();
        Car obj2 = (Car) obj1.clone();
        obj2.setColor("purple");
        obj2.setName("cloenCar");
        obj2.setType("NWD");
        System.out.println("clone car----->"+obj2.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        Car classLoaderObjeCar = null;
        try {
            classLoaderObjeCar = (Car) new SampleJavaProject().getClass().getClassLoader().loadClass("com.test.java.Car").newInstance();
            classLoaderObjeCar.setColor("purple");
            classLoaderObjeCar.setName("classloader car");
            classLoaderObjeCar.setType("MWD");
            System.out.println("Class Loader car----->"+classLoaderObjeCar.toString());
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Accord---->"+accord.toString());
        System.out.println("Tesla---->"+tesla.toString());
        System.out.println("Camry---->"+camry.toString());
    }
}

Here is the full video of the part 2 Base Java Series. Please show some love and subscribe to my channel Subscribe.

Top comments (0)