Thursday, May 25, 2023

Difference between Class and Object in Java and OOP with Example

Class and Object are the two most important concepts of Object-oriented programming language (OOPS)  e.g. Java. The main difference between a Class and an Object in Java is that class is a blueprint to create different objects of the same type. This may look simple to many of you but if you are a beginner or just heard the term Object Oriented Programming language might not be that simple. I have met many students, beginners, and programmers who don’t know the difference between class and object and often used them interchangeably.

 Also, Java API having classes like java.lang.Object and java.lang.Class also adds more confusion in beginners' minds. Both of them are totally different things, class and object in OOPS are concepts and applicable to all Object-oriented programming language e.g. C++ or Scala.

On the other hand java.lang.Class and java.lang.Object are part of Java API. Along with other OOPS concepts like Abstraction, Encapsulation, Inheritance and Polymorphism, this is also one of the most fundamental of Object-oriented programming (OOPS) which needs to be clearly understood before proceeding with the serious application programming. 

Without a clear understanding of Class and Object, you are more prone to make errors, not able to comprehend an already written program and it would be pretty hard for you to find bugs or fix errors or exceptions in Java code. 

By the way, the difference between class and object is also a popular programming interview question, which is quite frequently asked at fresher-level interviews.  In this article, we will look at different angles to differentiate Class and object in Java.

Once you get hold of key OOPS concepts, I would recommend reading 10 OOPS and SOLID design principles for Java programmer, which is a tried and tested design pattern for writing better code.




Difference between Class vs Object in OOPS and Java

Here is my list of differences between Class and Object in OOPS. Class and Object are related to each other because every Object must be the type of any class. In the same time class, itself is of no use until you create an object. Let’s see these differences between class and object in points :




1) Class is blueprint means you can create different objects based on one class which varies in their property. e.g. if Car is a class than Mercedes, BMW or Audi can be considered as object because they are essentially a car but have different size, shape, color, and feature.

2) A Class can be analogous to structure in C programming language with the only difference is that structure doesn't contain any methods or functions, while class in Java contains both state and behavior, state is represented by field in class e.g. numberOfGears, whether a car is automatic or manual, car is running or stopped etc. 

On the other hand, behavior is controlled by functions, also known as methods in Java e.g. start() will change state of the car from stopped to started or running and stop() will do opposite.

3) Object is also called instance in Java and every instance has different values of instance variables. e.g. in the following code

class Person {
    private String name;

    public Person(String name) {
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
}

Person p1 = new Person("Rakesh");
Person p2 = new Person("Jimmy");
Person p3 = new Person("Peter");

Here Person is a class as it defines the design of Person objects i.e. How will a person's object look like, what properties it will have etc. By the way, Class is declared by keyword "class" in Java and p1, p2, p3 are different objects of the Person class. 

In natural language, you can say different person which has different names where name is a property of Person Class. Another difference between Class and Object in Java is that we have a class keyword to declare a class in Java but there is no object keyword. Objects are most notably created using the new() operator, which calls the constructor of the class to create and initialize the object in Java.

And, if you prefer to see difference in tabular format, here is a nice table highlighting difference between a class and Object in Java:

Difference between Class and Object in Java and OOP with Example




What is difference between Class and Object in JavaThat’s all on the difference between class and object in OOPS and Java. As I said the main difference between class and object is that the former is a design while latter is an actual thing. The class specifies how an object will look like and object belongs to a particular type. In Object-oriented programming language, you can find real examples of class and object in your surroundings e.g. Home can be a class and everyone’s home can be considered object of class home because they are home but they are also different from other homes.


Other Object-Oriented programming tutorials from Javarevisited Blog

2 comments:

  1. Class is a strcuture which holds both your data (attributes or fields) with methods, which operates on that data together. You define class by using class literal in Java, and other OOP language like C++ and C# also has similar syntax. Class is extended duing Inheritance. Objects are created from Class. An Object always belong to a Class, without Class object cannot stay, but you can have class without object. Each object has their own data but share same methods from class. This data actually reflect state of object. Data chances means state of object changes. Two object are said to be equal if they have same state. Each programming language has different way to denote equality e.g. in Java you got == and equals() which provides reference and content equality.

    ReplyDelete
  2. One difference betweeen Class and Object is that you can use dot operator to access fields and methods on Object but can only access static members using Class and Dot operator.

    ReplyDelete