Class and Object are two most important concept of Object oriented
programming language (OOPS) e.g. Java. Main
difference between a Class and an Object in Java is that class
is a blueprint to create different objects of same type. This may looks
simple to many of you but if you are beginner or just heard term Object
Oriented Programming language it might not be that simple. I have met many
students, beginners and programmers who don’t know 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' mind. 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 into serious application programming.
Without 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 this on 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.
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 into serious application programming.
Without 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 this on 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

1) Class is blueprint means you can create different object based on one class
which varies in there 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
only difference 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 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 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
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");
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 design of Person objects
i.e. How will a person 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 object of 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 class in Java but there is no object keyword. Objects are most notably created using new() operator, which calls constructor of class to create and initialize object in Java.
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 class in Java but there is no object keyword. Objects are most notably created using new() operator, which calls constructor of class to create and initialize object in Java.
That’s all on difference between class and object in OOPS and Java.
As I said main difference between class and object is that former is a design
while later is actual thing. 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 to other homes.
Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming in Java
Java - Object Oriented Programming [For Absolute Beginners]
Other Object Oriented programming tutorials from Javarevisited
Blog
2 comments :
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.
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.
Post a Comment