You are on page 1of 4

ESSENCE OF CLASSES AND OBJECTS IN OOP In the world, if we see around ourselves, we always find things sharing common

property (ies), on the basis of which they can be grouped together and can be put into one set, like, vehicles, toys, furniture etc. Not only that the non living things, but the living things also share common trait(s) on the basis of which they have been classified into various kingdoms and further species. Here in this concept, the vehicle, the furniture, the toy, all form a class. Whereas, the type of vehicle - like car, scooter etc. - or the type of furniture - like bed, dining table etc. - form a subclass. However, the car number 68XY or U model bed are the examples of objects or instances. Whenever we talk about object oriented analysis and design, the concept of class is first and foremost. A class is not only a new way of defining and implementing the data types by users but it is also a mean to model the real world behavior. Classes are the actual modeling of the object-oriented concept. The object can be visualized as A CONTAINER THAT CONTAINS BOTH DATA AND CODE TO MANIPULATE THE DATA. Objets and classes are interrelated to eachother as A CLASS IS A COLLECTION OF SIMILAR OBJECTS. As we saw earlier that the class is a user defined data type, hence the entire set of data and code of an object can be made a user defined data type with the help of class. Every object is directly related to a class. Objects are nothing but instances of class. A class can be defined as a collection of objects of similar type. Thus a class is generalizationof objects having similar attributes and operations. A class consists of two parts, one isclass data types and the other is class data functions. These are collectively called classmembers. Within a class, the visibility level of class members is very important.. A class member can be one of the two types: PRIVATE or PUBLIC. As the name suggests, a private member can be seen only inside the class, whereas the public class member can be seen, accessed and mutated outside the class as well. A class is an ABSTRACT DATA TYPE since it follows the concept of ABSTRACTION. Abstraction is the mechanism, which only shows the necessary information to the user, while hiding background details or implementation. Rotation of a fan by turning on an electrical switch, without any additional effort or information requirement, is clearly an example of abstraction. Then, we come to an important concept of the object-oriented analysis, which is DATA HIDING AND ENCAPSULATION. Both of these terms are tightly interrelated and are very similar to each other. Data Hiding is a very broad term that means insulating the data such that is cannot be directly accessed by the program. Encapsulation is a specific method of this term in which we wrap up both the data and the function in a single unit (class).

Importance of Class and Object Oriented Programming While class is certainly important to have in real life, it is equally important to have while programming. Taking advantage of an Object Oriented Programming method (or OOP method) may very well be one of the most important higher concepts to learn and understand. This article helps cover what OOP is, its benefits, and how to use it. Objects In the real world each thing, or object, acts independently of everything else with no interconnected reliance. Objects can react to other objects, but do not necessarily depend on other objects. Note: there are objects which are entirely dependent on other objects, but we'll get to that. If you have just started programming you likely will have noticed that the program as a whole runs in a very linear fashion with events occurring in a very specific order. To get around this many programming languages have implemented a class based architecture. Even Objective-C has structs which function in a similar manner. Class What is a class exactly? Succinctly, a class is whatever you want it to be. At its most basic level, a class is an object that you define yourself. What a class does is entirely up to you, but you will want to put some thought into constructing it to make sure it behaves as you intended. Classes allow you to group functions, variables, and even other classes into one contained object that does not interfere with other objects. Interaction principles Does not interfere? What if it needs to? Well, that's where access identifiers come in. They let you determine what can access the particular variable/function. There are three main access modifiers, and one that is less well known. The three main modifiers are (ordered by access level):

Public Protected Private

The last modifier is internal, it is not very well known, and has limited to no use in smaller projects and therefore will only be covered briefly.

Public Access First is the public access modifier. Public variables, functions, classes, etc. Can be accessed and modified by anything and everything. Some will say that one should never have public data, and others will say to use it with extreme caution. I fall in the latter camp personally. Ideally you only ever want to have a variable public if you want it to be modified by an external process, otherwise it is better to have it be a protected or private variable and then define a public function that allows the data to be read by other processes. To some this does seem much, but it all depends on your security needs, and whether or not you are the only one who will ever work on the project. A good rule of thumb is, "if you do not want it modified by just about anything, don't make it public." One exception is in Unity where a variable must be public in order to allow modification of said variable within the inspector tab(s). Protected Access Protected methods can be accessed by the class they are defined in, as well as any other class that is built on top of the class. This brings us to the topic of inheritance, which will be covered briefly before moving on to the private and internal definitions. Inheritance To better understand inheritance we need to look at a real world example. Chances are you know what a car is, what it does, and generally how to use one. Most cars function the same, but yet are very different. One main difference is Manual vs. Automatic transmission. Aside from that there are sports cars, luxury cars, commercial vehicles, and many many more. Yet these are all cars, and they all have many things in common: wheels, brakes, an engine, the need for gasoline, a battery, drive shaft, steering wheel, lights, horn, and on and on. To represent this in programming you would create a car class, then you would probably make several other classes based on these for the different vehicle types. To do this you would use the extends keyword followed the class which this class is to be based upon. This new class will have access to absolutely everything that its parent has access too, except private members. As mentioned above, protected members can be accessed only by a class and its sub-classes. Private Access This brings us to the last of the three main access modifiers: private. The private modifier lets the defined method only be accessed by the class itself, and absolutely nothing else, not even derived classes. Taking the real-world car example, the basic car class would probably define how the engine works to produce thrust, but any sub-class would simply access the accelerate function instead of directly modifying the various engine components required to make acceleration happen. Thus, opening the valves, controlling airflow, fuel injection, ignition sequence, and exhaust are private, and for good reason too. Imagine having to control all of that every single time you wanted one of the cars to accelerate, instead of just pushing a handy little pedal.
3

Internal Access The internal modifier is not very well known because it limits access to the assembled package. This means that only this program, and no other program, can access the method. This can be handy if you are building a DLL file and want certain methods public within the DLL, but not available outside of its local program scope. Reading that definition may make you wonder why internal is needed at all since most everything is limited by its scope. The key thing here is classes. In particular you want a class accessible within a DLL for instantiation, but not available outside of it because the name it uses might clash with something else, or because it is a security concern. As aforementioned, this modifier is used very little in smaller projects. Construction Tips "That's all well and good," you might say, "but how do I actually use classes in my workflow?" There are a few things to keep in mind when constructing a class, and it can help to diagram out what is to do what well ahead of actually implementing anything. This will prevent you from having to come back and restructure the class an untold number of times. Instantiation Classes typically require a public function of the same name as the class which acts as the instantiation method. What this means is that when you define "ClassName obj = new ClassName(a, b, c);" the parameters listed by your favorite IDE are pulled from the function with the same name as the class you are creating, which in this case is ClassName. It is possible, at least in every language I have encountered besides Objective-C, to have multiple, or overloaded, functions with the class name. This will allow for different ways to create the object depending on the circumstances and the amount of information actually required. Update In many cases it is also beneficial to have at least one commonly named update function whose function name exists in all necessary classes to provide a simple means of running standard loop logic. That way in the main body of the program you could just iterate through all objects and call their update functions, if they exist. Input Likewise it is a good idea to come up with a naming convention for various types of input. Windows allows you to register a listener function for keyboard, mouse, and other input. Within the function it would be very easy to call the respective listener functions of everything in your program if they all had similarly named listener functions.

You might also like