Object Oriented Programming is confusing concept to many beginners. Terms related to classes and objects seems overwhelming, and sometimes even frustrating. In this tutorial I would try to generalize OOP concept and try to show how OOP can be extended to various programming patterns. Overall, I will show how to begin with the concept of OOP; build firm concept; apply to various programming tasks with the aim to write more manageable code.
OOP is the real life way to manage data. It just tries to held similar process and its data as one entity which makes us easy to write code. Main understanding that each individual needs to make is, OOP doesn't increase performance of code in machine but rather facilitate developer to maintain code in long run.
Let's begin with introduction to OOP. OOP is the programming pardigm that introduces basic idea that model (take human body where variables are eye, ear) and process (take walking, eating, running) should be part of same module. In this way data can be used when process needs it. Keeping it more simple, I would say you require data (e.g, legs, eye, hands for running) when activities have some logic involved (eyes on the road, fast movement of legs), which we will code inside process. Process are known as functions/method in aspect of OOP. It is just the next way that came after procedural programming (C Programming Language is great example) where data used to be initiated whenever required.
Where does OOP stand?
Whenever we write code, we need to understand where that code stands in reference to hardware and how it is operated. In direct terms, how closely does the language communicate with Hardware? C++, Java and C# are examples of great OOP Languages but, all do not follow same architecture. I will discuss specific Programming Languages Architecture in different topic in detail, although knowing little about architecture will turn helpful to understand overlying structure. C++ is new brother of C. With all major aspects of C and implementation of OOP, C++ seems to work closely with Hardware. Due to this performance comparatively high. It's like manager in resturant directly cleaning table, rather then calling waiter and ordering the task. The pointers still exists in C++ means it somewhat couldn't leave direct access to memory questioning, if C++ is fully OOP? Pointers are the term popularly used in C to have direct access to memory and it's address. Java stands above JDK and known to be complete OOP. It's performance is well although java code is first converted to object code and further processed by machines. Due to this concept it became able to cross platform tool to write code and became popular in entreprise world. C# lies above .NET Framework and with great syntax structure fighting to be king of OOP. There are other various OOP but in one way or other follows similar concept in these three language. Mastering these three language should trace a path to understand any OOP language quickly.
Major Concepts in OOP.
OOP stands on four major pillars given below. Although there are more concepts added with new OOP languages these concept will overall cover the major aspect in any OOP .
A. Class and Objects: OOP core idea is to match real world experience with classes and objects. What can be classes? Everything you can imagine of like: Car can be imagined as model and its various parts as its attributes.
Imagine OOP is like mass production of of some brand of car, we have blue print of the design (known as classes) and whenever we want to create a new Car object we just declare that we need a car object then compiler acts as car plant to create a car that is ready to use. What if we need car of different color? The work can be defined like; that you need car that is with different color with all other things kept same!
Typical declaration of any class can be seen below:(Note: This structure is general concept to define class. Different programming language may have different implementation in terms of syntax)
// some attributes
dataType variable: _____________;
// some working statement
Typical definition of object from class is given below:
ClassName classVariableN = new ClassName(); // here object is class variable (N is show that we can define various objeccts from a single class)
This part was written to clear your concepts and classes and objects. Just I have left one part so, that you might be curious to search one thing that is really important. i.e. how properties of class can be altered? There is concept of Constructors, where we can pass parameters to define classes with modified property. There are plenty of resources in the internet, just try to spend some time on this topic.
B. Inheritance: Inheritance in programming follows similar concept in biology. Inherit as the word means is to get by some relation. In OOP Programming, these relation are Parent/Super Class and Child/Sub Class. All inherited property can be used in child class. If I define any function in Super class then the objects following the sub class become able to use super functions and thier class functions during programming. Personally, I feel this is the most easy concept in programming. I have put down a example to explain the concept in some programming terms.
// statement lines
class SubClass inherits SubClass
In above example SubClass inherits SuperClass. All objects that are created from SubClass can access sampleFunction() although it is not available in SubClass. Due to inheritance it is automatically taken as function of SubClass.
C. Encapsulation: Encapsulation also known as data abstraction or data hiding is mechanism to hide things. Most beginner see it as the security mechanism which it completely wrong. Encapsulation is the way how we(developer) try to limit the scope of data.
Encapsulation can be understood by relating it with an office. Imagine there are only three members in office. i.e. Boss, Manager and Office Assistant. Boss acts as private variable, that would not move here and there freely; rather try to take more safe and guarded areas. Manager would try to show similar properties to boss but try to be more open to environment which can be compared to protected variables. And Office Assistant can be treated as public variable who moves here with some kind of files, tea and sometimes gossips which can be accessed by anybody. The main theme we can draw from this concept is the openness of data or its access.
Typical way of using encapsulation is given below:
scopeVariable dataType Variable;
scopeVariable dataType Function()
// some working statement
There are three keywords that defines scope:
a. Private: While declaration of any variable or method inside class we can use private keyword to orgainze less scope of access of data. Private variables and function are only accessible inside that class where they are declared.
b. Protected: Protected are declared with protected keyword and it can be accessed by inherited classes. For example, If Add() is declared in Super/Parent class then the method can be used in Sub/Child class. This provide moderate scope for data access.
c. Public: Public are accessible from anywhere. This variables/methods have high scope in terms of access.
The major question here comes is that, when are anybody required to use private, protected or public? I would make it easy, in smaller programs that you do in college, it doesn't matter so much, as you can get quick results with public declarations. But, in industrial programming or like building huge projects, you would declare all variables private and access required variables with public function (popularly known as setters and getters). At the end you will end making public getters and setters for only those variables that needed to be accessed outside the class scope. For extra part, and to extend your knowdge make research on setter and getter functions.
D. Polymorphism: This terms refers to taking different forms. We can define any kind of function in one class and later change it to work differently in extended classes. For example, we have defined a sampleFunction in the ParentClass; we would have ablity to modify this function in ChildClass. Polymorphism extends to concept called constructor overloading and function overloading. In C++ we have also concepts of operator overloading. I have shown example below that should be able to give you brief understanding of function overloading.
scopeVariable dataType sampleFunction(); // doesn't have any parameter
scopeVariable dataType sampleFunction(int a); // have one integer paramenter
In above example we have two sampleFunction. First one doesn't take any parameters while second one take integer value as the parameter. This shows how function can be overloaed to solve our needs when we have to run simlar objective with different type of values.
The concepts given above were basic concepts. OOP is large topic and these concepts willl be extend in my further writings. As this was intended for people who have zero knowledge on OOP, I had tried to make it as simple as I could. Still if you have any queries, just open contact form and submit your query. I will get back to you as soon as possible.