Object-Oriented Programming (OOP) is a programming model where programs are organized around objects and data rather than action and logic. OOP allows the decomposition of a problem into several entities called objects and then builds data and functions around these objects. Using the blueprint analogy, a class is a blueprint, and an object is a building made from that blueprint.
Let define a class: A class is the core of any modern Object-Oriented Programming language such as Java. In OOP languages it is mandatory to create a class for representing data. A class is a blueprint of an object that contains variables for storing data and functions to perform operations on the data. A class will not occupy any memory space and hence it is only a logical representation of data.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Circle { double radius; String color; double getRadius() { } double getArea() { } } |
Let define an object: Objects are the basic run-time entities of an object-oriented system. They may represent a person, a place, or any item that the program must handle. The software is divided into several small units which are an instance of a class called objects. An object is a software bundle of related variables and methods. The data of the objects can be accessed only by the functions associated with that object.
1 2 3 4 5 6 7 8 9 10 |
Circle c1, c2, c3; // They hold a special value called null // Construct the instances via new operator c1 = new Circle(); c2 = new Circle(2.0); c3 = new Circle(3.0, "red"); // You can Declare and Construct in the same statement Circle c4 = new Circle(); |
Difference between object and class:
No. | Object | Class |
1) | An object is an instance of a class. | Class is a blueprint or template from which objects are created. |
2) | The object is a real-world entity such as a pen, laptop, mobile, bed, keyboard, mouse, chair, etc. | Class is a group of similar objects. |
3) | The object is a physical entity. | The class is a logical entity. |
4) | The object is created through a new keyword mainly e.g. Student s1=new Student(); | Class is declared using class keyword e.g. class Student{} |
5) | The object is created many times as per requirement. | The class is declared once. |
6) | Object allocates memory when it is created. | Class doesn’t allocate memory when it is created. |
7) | There are many ways to create objects in java such as new keyword, newInstance() method, clone() method, factory method, and deserialization. | There is only one way to define a class in java using the class keyword. |
The article was published on August 8, 2017 @ 4:59 PM
Leave a Comment