The most important principle of object-oriented programming is Classes and Objects.
A Class is a group of things(objects) which have similar properties or behaviour. For example - "Fruit" is a class. Why? because the word "Fruit" does not specify a particular fruit but all the types of fruits. In real life there is nothing called a "fruit", which means that if you go to a fruit seller and tell him that - I want to buy a fruit - he will ask you to specify which fruit exactly do you want to buy. All the examples of a "fruit" like Orange, Banana, Apple or any other are instances of simply examples of "fruit", that's why they are called "Objects". Objects are found in real world not the class. Classes are simply created to refer to all the similar objects easily.
The method invoked by an object in response to a message is determined by the class of the receiver. All objects of a given class use the same method in response to similar messages. Apple is an instance of a category or class of Fruit i.e. Apple is an instance of a class of fruits. The term fruit represents a class or category of all fruits. We interact with instances of a class but the class determines the behaviour of instances.
We can tell a lot about how Apple will taste by understanding how fruits taste. We know, for example, that Apple, like most fruits will have juice and will taste sweet.
In the real world there is this distinction between classes and objects. Real-world objects share two characteristics: They all have state and behavior. For example, dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Students have state (name, student number, courses they are registered for, gender) and behavior (take tests, attend courses, write tests, party).
Basics of Objects and Classes
We move now from the conceptual picture of objects and classes to a discussion of software classes and objects. Objects are closely related to classes. A class can contain variables and methods. If an object is also a collection of variables and methods, how do they differ from classes?
Objects and Classes
Objects
In object-oriented programming we create software objects that model real world objects.Software objects are modeled after real-world objects in that they too have state and behavior. A software object maintains its state in one or more variables. A variable is an item of data named by an identifier. A software object implements its behavior with methods. A method is a function associated with an object.
Definition: An object is a collection of variables and related methods. An object is also known as an instance. An instance refers to a particular object. For e.g. My car is an instance of a car—It refers to a particular car. Allen is an instance of a Student. The variables of an object are formally known as instance variables because they contain the state for a particular object or instance. In a running program, there may be many instances of an object. For e.g. there may be many Student objects. Each of these objects will have their own instance variables and each object may have different values stored in their instance variables. For e.g. each Student object will have a different number stored in its StudentNumber variable.