OOP Fundamental — For Dummies

Simple Guy
8 min readApr 4, 2020

--

Object-Oriented Programming

Forewords

For every new person who started to learn the art of programming, I can tell they might heard from somewhere about Object Oriented Programming (OOP). So what is it ? Why nowadays developers are talking and practicing about the OOP ? For this series, we gonna discussing about OOP and then later relates to S.O.L.I.D principles, even Design Patterns. I am mainly using .Net C# for this series.

Who should read this ?

  • If you are studying, majoring software engineering
  • If you are fresh graduated student, working closely/in software development
  • If you are an experienced developer and want to deep dive into OOP

Even if you are NOT software developer, you should read this to learn about the logic, to reach new “OO” views related to our life.

I wish I knew …

It would be a good point if you are have programming knowledge, if not, don’t worry about that. Although this article I will deep dive a little bit but I believe you can learn the OO concept by your self. Beat it.

Goals

First of all, we are trying to reach the concept of OO; then the second one is trying to explore what actually OOP; And finally, we will try to master “this”.

Now, let’s start !!!

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods) — Wikipedia

Above is the definition of OOP by Wikipedia. So we gonna figure out what is “objects”.

Everything in Objects. Yes, definitely.
Person is an object. Dog is an object. Rock is an object, also.

In real life, an object contain their information (data) and has their own behaviors (in form of procedures). For example:

Information (data):

  • Virus has their own data: DNA, name, …
  • A Person has their own data: Full name, height, weight, arms, shape, specific identification, …
  • Dog’s data : Eye, nose, legs, …
  • Rock’s information: material, weight, color, …

Behaviors:

  • A Person has their own behaviors: walk, cry, drink, …
  • Dog’s behaviors: run, make noise (woof woof), eat, …
  • Rock behaviors: Wait! Rock don’t have their behavior …But, if a person throwing rocks, it would cause damage to the dog. So we could say the rock can cause damage (attack). It’s behavior.
  • Also for the virus, virus might cause symptom to you.

In programming, the information (data) often knows as Properties, and object’s behaviors often knows as method or function.

Okay ! So what actually is Object-Oriented Programming ?

One answer to this question is “The combination of data and function.” Although often cited, this is a very unsatisfying answer.

Another common answer to this question is “A way to model the real world.” This is an evasive answer at best. What does “modeling the real world” actually mean, and why is it something we would want to do? Perhaps this statement is intended to imply that OO makes software easier to understand because it has a closer relationship to the real world — but even that statement is evasive and too loosely defined. It does not tell us what OO is.

There is four magic words about OOP is:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Some say that OO is the proper admixture of these four things. So we will try to examine each of these concept in turn. and try to understand what actually OOP is by ourselves.

OOP’s four magic words

Encapsulation

OO languages provide easy and effective encapsulation of data and function. But this idea is not unique to OO. Indeed we had perfect encapsulation in C. Let’s see simple C program.

brick.h
brick.c

The user of brick.h have no access whatsoever to the members of struct Brick. they can call makeBrick function but they have absolutely no knowledge of the implementation of either the Brick data structure or the functions. This is the perfect encapsulation — in a non-OO language.

But then came the OO in the form of C# — this perfect encapsulation of C was broken. Java and C# simply abolished the header/implementation altogether. In these language , it is impossible to separate the declaration and definition of a class.

The way encapsulation is partially repaired is by using private, protected, public keyword into the language called Access Specifiers. These are many more access specifiers, not just private, protected, public. But for simple concept, we will just have a look on these three access specifiers scope.

  • Private: this is the least permissive access level. The scope of accessibility is limited only inside the class or struct in which they are declared. These private properties cannot be accessed outside the class.
  • Protected: the scope of accessibility is limited within the class or struct and the class derived (Inheritance) from this class.
  • Public: this is the most common access specifier. There is no restriction on accessibility so it can be access from anywhere, both inside and outside the class, also can be access by any other code in same assembly or another assembly that references it.

So far, we know more about these access specifiers (access modifiers). Keep playing around with these access modifier, you will be figure out the benefit of encapsulation by yourself.

Abstraction

Abstraction basically is about the concept or an idea which “hides” unnecessary details, actual implementation from the class. In C#, abstraction accomplished using abstract classes, abstract properties, abstract methods and interfaces.

We let’s discuss about the activity. Activity is an abstract definition. Eat is an activity; Sleep is an activity; Play video game is an activity; Running is an activity. If I asked you about your daily activities, I am absolutely don’t know what actually are your “activities”, but your individual activity know it.

Ok, so far we got how to create and abstract class and derived classes from it. We know how to implement details and function by using override keyword (see Eat, Sleep, Play classes, overriding ActivityName and DoActivity() function). By using this, you can write code for actual implement within the class derived from abstract Activity class.
Let’s create a small program to see how it “hides” the actual implementation.

Greats! Clients code only know about DoActivity() method, and know nothing about the implementation for each derived activity.

We could say: one class should not know the inner details of another in order to use it, knowing the interfaces is good enough.

Inheritances

If the OO-languages did not give us better encapsulation, they gave us inheritance. Sort of, Inheritance is simply the re-declaration of a group of variables and functions within an enclosing scope. Let’s have some simple example.

Person is a base class. Teacher and Developer (and maybe others) classes Inherited Person class. Dancer class Inherited Teacher. So following the diagram above, a class derived from another classes will have their parents (super) classes skills + their own new skills. In Java, we using “extends” for inheritance and “implements” for interface implementation. In C#, we just using “:” to let the compiler know that some class inherit/implement from base class. Let’s see some code.

So let look closely into Program.cs code file.

  • Teacher can reuse Person’s skills because it inherited from Person class
  • Developer can reuse Person’s skills because it inherited from Person class
  • Dancer can reuse Person’s skills, also Teacher’s skills because it inherited from both Person and Teacher classes.

We could say: Inheritances expresses “is-a” and/or “has-a” relationship between two objects.

Polymorphism

Polymorphism meaning “one name, many forms”. One object has many forms or has one name with multiple functionalities. Polymorphism is powerful principle in OO-language, it provides the ability to a class to have multiple implementations with same method name.

There are two types of Polymorphism

Two types of Polymorphism

Static / Compile Time Polymorphism (a.k.a Early Binding):

Overloading methods is one example for compile time polymorphism. Overloading methods/ function has the same name but different signatures. C# compiler checks the number of parameters passed and the type of parameter and make the decision which method is to be called is made at compile time. Here some simple code:

There is only one method named Add, but there is 3 different signatures. When client try to call Add method, compiler will show all 3 signatures that client can be use.

Dynamic / Runtime Polymorphism (a.k.a Late Binding):

We have already know about inheritance. Method overriding is an example of dynamic polymorphism. With method overriding it is possible for the base class and derived class to have the same method name and same something. The compiler would not be aware of the method available for overriding the functionality, so the compiler does not throw an error at compile time. The compiler will decide which method to call at run-time and if no method is found then it throws an error.

Let’s look Inheritance code sample again and try to explore dynamic polymorphism by yourself.

Additional, we can user “virtual” keyword to implement base logic from super class, and implement polymorphism method in derived class. If the derived class “overriding” virtual method from super class, in run-time, overriding method will be executed, or else the default method from super class will be used.

Conclusion

We have go through these OOP principles, but there is more interesting things hiding under these knowledge. Practicing made a champion. Based on these knowledge, you need to keep coding and deep diving into it by yourself. I gave you some tools, you must master these. One day, when you realized you can follow S.O.L.I.D, coding Design Pattern and much more by just using your own OOP knowledge, you could say you mastered Object-Oriented programming.

Ask question. Don’t be selfish.

I also have three questions for you:

  • Does abstraction is actually one of OOP principles ? Why ?
  • What is the different from Abstract class and Interface ?
  • Have your ever heard about Dependency Inversion ? Why it related to this OOP concept ?

P/S: Please feel free to give me question as comment below.

References

While writing this article, beyond my own knowledge and experiences, I have researched and followed some C code from Clean Architecture (Robert C. Martin Series) book.

--

--