Learn about Classes constituents of Object Oriented Programming
Classes are the most important constituents of Object Oriented Programming.
A class is just a template which contains the various attributes and functions of an object of the class.
Consider a class bird.
A bird class will have the following attributes:
It will also have the following functions
A class gives you only the attributes.
If you ask 10 different persons to think of a bird and describe, each one will describe it differently. Because it is just a general name.
An object is a run-time entity of a class.
Say, you create an object parrot from the class bird. Then you know the value for all the parameters for this particular object. If you create another object duck, then this object will also have all the parameters defined for the class bird. But, the values for the parameters would be different from that of the object parrot.
Another example :
here, I can think of 'a' as an object of the class 'int'.
|Author: Kamlesh Mohanty 28 Dec 2011||Member Level: Gold Points : 1|
|>>Class is a logical representation of data with related methods|
>>Object is a physical representation of data based on class and it is called as instance of class
eg.Human being is class whereas any person is an object
|Author: ketan Italiya 24 Sep 2013||Member Level: Gold Points : 5|
In programming terms, an object is a self-contained component that contains properties and methods needed to make a certain type of data useful. An object’s properties are what it knows and its methods are what it can do. The project management application mentioned above had a status object, a cost object, and a client object, among others. One property of the status object would be the current status of the project. The status object would have a method that could update that status. The client object’s properties would include all of the important details about the client and its methods would be able to change them. The cost object would have methods necessary to calculate the project’s cost based on hours worked, hourly rate, materials cost, and fees.
In addition to providing the functionality of the application, methods ensure that an object’s data is used appropriately by running checks for the specific type of data being used. They also allow for the actual implementation of tasks to be hidden and for particular operations to be standardized across different types of objects. You will learn more about these important capabilities in Object-oriented concepts: Encapsulation.
Objects are the fundamental building blocks of applications from an object-oriented perspective. You will use many objects of many different types in any application you develop. Each different type of object comes from a specific class of that type.
Classes, instances, and instantiation
A class is a blueprint or template or set of instructions to build a specific type of object. Every object is built from a class. Each class should be designed and programmed to accomplish one, and only one, thing. (You'll learn more about the Single Responsibility Principle in Object-oriented programming concepts: Writing classes.) Because each class is designed to have only a single responsibility, many classes are used to build an entire application.
An instance is a specific object built from a specific class. It is assigned to a reference variable that is used to access all of the instance's properties and methods. When you make a new instance the process is called instantiation and is typically done using the new keyword.
Think about classes, instances, and instantiation like baking a cake. A class is like a recipe for chocolate cake. The recipe itself is not a cake. You can't eat the recipe (or at least wouldn't want to). If you correctly do what the recipe tells you to do (instantiate it) then you have an edible cake. That edible cake is an instance of the chocolate cake class.
You can bake as many cakes as you would like using the same chocolate cake recipe. Likewise, you can instantiate as many instances of a class as you would like. Pretend you are baking three cakes for three friends who all have the same birthday but are different ages. You will need some way to keep track of which cake is for which friend so you can put on the correct number of candles. A simple solution is to write each friend's name on the cake. Reference variables work in a similar fashion. A reference variable provides a unique name for each instance of a class. In order to work with a particular instance, you use the reference variable it is assigned to.