Object orientation
Object orientation is a philosophy when building applications.
The four concepts of object orientation
Object oriented design, analysis and programming builds on four core ideas.
- Abstraction
- Polymorphism
- Inheritance
- Encapsulation
Abstraction
Not defining any specifics.
Abstraction is about having an idea of an object without knowing exactly how it looks.
For example a chair. You know what a chair can do (behavior/action) and which attributes it has (legs, color, material and so on). So when talking about a chair in conversation nobaody needs to talk about exactly how it looks or are used bcause that is already understood and separate from the chair talked about.
In fancy words this can be described as: The abstraction of a object is the opposite of a specific instance of that object. So while it has some expected behaviors and attributes those are general for the object nothing is set and described.
This is a bit like normalization when designing data models. For example you would not make a table for each object in a database. When the rows/records share properties they all can be in the same table toghter. This applies to classes in object oriented programming as well.
Polymorphism
“Many forms”
It is about flexibility and doing the right thing at the right time.
As an example think about linking Css files with media queries. Depending in which order they are linked they overwrite the ones above when necessary. In other words: if some conditions are true the site is showed in a different way.
This applies to classes as well. A subClass that has the same method name as its parent, overwrites the method in the superClass which makes the method change in nature based on that.
Another example: + means sometimes concatenation and sometimes addition depending on the situation.This is polymorphism in its simplest form.
Inheritance
Inheriting attributes and behaviors.
Classes (objects) can inherit from other classes. That makes code reusable and you have to type less.
This is very similar to when designing a database. You always want to avoid duplicate columns that holds the same data. Instead move the common columns to one table (one class). That class will be the super class to other classes that inherit its properties and become sub classes. Other names for this are parent and child.
Inheritance in c#
public class Album : Product { }
In the example Album inherits behaviors and attributes from the super class Product.
Abstract class
An abstract class exist purely to be inherited from. It can be seen as a model which the inheriting classes copy and then add their own attributes and modify behaviors.
public abstract class Album { }
The example above are for creating an abstract class (public) in C#.
C# Reference: abstract
Interface
Very similar to a class but has no behaviors of its own. It consist of only method signatures.
Abstract class vs. Interface
Encapsulation
Making things modular.
Information hiding or data hiding. Encapsulation is about dividing attributes and actions into classes where they can be made public or be hidden. An object should only show the things that are absolutely neccesary for the application to work.
Think of this as a black box. It has input and utput. How the output is generated is hidden and you can never access the mechanics inside the box from the outside.