Fundamentals of OOP - Classes and Objects
- As the name suggest Object oriented programming is based on "objects" which is nothing but an instance of a class. Class contains fields often called attributes and blocks of code in the form of methods. So we can say object comprises of attributes and methods. In simple word, class is a blueprint for object and defines structure of object.
- By creating classes we categorize the data we are representing and give these representations a context by associating them with features in the programming languages API. However a class is never used in the main program, it must first be instantiated as an object and the object is what we would use in our main program. In this sense a class is more like a blueprint, that describes the complex relationships between the data we are representing, like the blueprint of a house can describe how tall a wall will be or how far a window will be from the ceiling. If you can imagine a class to be like a blueprint then a software object is like a house made from that blueprint. For example, a class only describes the possible objects that can be instantiated from it, taking the blueprint analogy further we cannot live in a blueprint yet it has all the information we need to build a house which would be something that we use as a functional object. In other words we use the blueprint to build a house, this is just like instantiating an object from a class. The class only describes the possibilities of an object that can be instantiated from it and when we finally do instantiate an object from the class, it is the object that we use in our main program.
- Real world example:
See the following image where we can see, the blueprint defines the structure of house which will help us construct the house. We can build as many houses(Objects) with different properties(fields) as we want just by changing the values.
Blueprint example |
Note(image taken from lyndondaniels.com)
0 comments:
Thanks for your Feedback !!!