Concept of Classes and Objects
Classes and objects are integral parts of the Object Oriented Programing (OOP) paradigm. This topic describes how they are related to each other.
Class: It is the blueprint from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.
Object: It is a runtime entity and its state is stored in attributes and behavior is shown via methods. Methods operate on an object’s internal state and serve as the primary mechanism for object-to-object communication.
Difference between Class and Object
According to Grady Booch, founder of OOP design principles, an object can be defined as: “An object has state, behavior, and identity; the structure and behavior of similar objects are defined in their common class; the terms instance and object is interchangeable.”
From these two definitions we can see that there is a difference between a class and an object. A class is the definition, or blueprint, for an object. Classes don’t real ever exist, they are just plans. We use the class to create an object which has identity. That is, an object exists, has values in its properties and can execute behaviors.
This difference between a class and an object is a subtle but very important one. It is analogous to visiting an architect to have a new house built. The architect will draw up plans for the house that show all the rooms and elevations etc. However, you cannot move into the plans and live there. The plans must be used to build a house. The house is an object while the plans are a class.
Characteristics of OOP
It is necessary to understand some of the concepts used extensively in object-oriented programming. It includes following characteristics:
It is the mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse. In short it isolates a particular code and data from all other codes and data. A well-defined interface controls the access to that particular code and data. The act of placing data and the operations that performs on that data in the same class. The class then becomes the ‘capsule’ or container for the data and operations. Storing data and methods in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those methods which are stored in the class can access it.
Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes.
Data Hiding/Information Hiding
Encapsulation provides the hiding of data/information. It prevents users from seeing the internal working of an object. The main reason for doing this is to protect data that should not be manipulated by the user. This makes the code more reliable and reusable.
It is the process by which one object acquires the properties of another object. This supports the hierarchical classification. Without the use of hierarchies, each object would need to define all its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. The new class is known as sub class / child class/derived class. The existing class is known as super class / parent class / base class. A new sub-class inherits all of the attributes of all of its ancestors. Inheritance is basically used for reuse the code and focus to make generic kind of thing rather than specific one.
Polymorphism means the ability to take more than one form. An operation may exhibit different behaviors in different instances. The behavior depends on the data types used in the operation. There are basically two types of polymorphism. Compile Time (also known as Early binding) and Run Time (also known as Late binding) Polymorphism.
In Compile Time Polymorphism, object knows about itself at compile time. Method overloading is an example of compile time polymorphism. In Overloading method should have same name with different arguments.
In Run Time Polymorphism, object does not know about itself at compile time it assigns all the properties and methods at runtime. Method overriding is an example of of run time polymorphism. It is related with inheritance concept.