Object vs class:
No. Object Class
1) Object is an instance of Class is a blueprint or template from which
a class. objects are created.
2) Object is a real world Class is a group of similar objects.
entity such as pen,
laptop, mobile, bed,
keyboard, mouse, chair
etc.
3) Object is Class is a logical entity.
a physical entity.
4) Object is created Class is declared using class keyword e.g.
through new class Student{}
keyword mainly e.g.
Student s1=new
Student();
5) Object is created many Class is declared once.
times as per
requirement.
6) Object allocates Class doesn't allocated memory when it is
memory when it is created.
created.
7) There are many ways to There is only one way to define class in java
create object in java using class keyword
such as new keyword,
newInstance() method,
clone() method, factory
method and
deserialization.
Structure Vs Class
Similarities and Differences
C++ structure Class
User defined User defined
Complex data type Complex data type
Collection of variables and functions Collection of variables and functions
Members are private/public/protected Members are private/public/protected
By default members are public By default members are private
Structure can’t be inherited Class are inherited
Used to build simple program/data types Used to build complex/large program/data
types (super market application, railway
reservation etc.,)
Class
It is a user defined data type
It is a complex data type
It is a collection of members
Data members (variable)
Member functions (function)
Class is a container
Class provides the concept of encapsulation
Class provides the concept of Data hiding with private member
Class is a blue print (original copy) to construct object
i.e. It is a plan before constructing objects
Class is logical copy to create objects (it doesn’t allocates memory, object only creates memory)
Syntax of a class
Example program for class (with data members and member function)
- Class definition with private data member and public member function (member
function defined within a class – inline function)
- Class definition and member function defined outside the class definition (here
member function defined with scope resolution (::) operator