C#: The core ideas of Object-Oriented Programs

This entry is part 18 of 71 in the series C#

The core ideas of Object-Oriented Programs are Encapsulation, Inheritance and Polymorphism.

Encapsulation

The idea behind encapsulation is that you want to keep each type or class discrete and self-contained so that you can change the implementation of one class without affecting any other class.

The class that provides methods or functions that other use is usually called the main class. You can declare an instance of the class and make use of the member functions and types of the main class.

As your member functions in a class can be used by other class members, you want to provide some restrictions to those classes by declaring them public or private.

Inheritance

If you need to declare an instance of the class that is slightly different from the main class, you can derive a class from the main class or in this case the parent class.  The derived class is called the child class.  This  is called inheritance in Object-Oriented programs.

The inheritance relationship is referred to as the is-a relationship. A dog is a mammal; a car is a vehicle. (Dog would be derived from the main class Mammal and Car from the main class Vehicle.)

If you change how a shared ability is implemented in the main class, you do not have to update code in every derived type; they inherit the changes.

Polymorphism

Polymorphism, the third pillar of object-oriented programming, is closely related to inheritance. The prefix poly means “many”; morph means “form.”

Thus, polymorphism refers to the ability of a single type or class to take many forms.

A way to understand polymorphism is to look at the + operator.  It depends on the context that the + operator is used.

The + operator can be used arithmetically as in 5+3 = 8.  It can also be used to add a string together such as “hello” + “world” to get “hello world”.

C#: Learning An Object-Oriented Programming Language

This entry is part 9 of 71 in the series C#

C# is an object-oriented programming language.  This is very different from the traditional procedural languages such as C.

Its been said that if you have been a C programmer, it might take you 6 months to unlearn procedural style of programming and train yourself in writing programs the object-oriented style.

For a new programmer, the best approach is to learn an object-oriented language sucn as C# directly instead of learning a traditional language first.

The first thing is to understand that look at the world as if they are all objects.  For examples, a car, an animal or a company.

There are certain characteristics or properties that are associated with objects such as the size of a car or the number of employees in a company.

How do you manipulate or modify those properties?  You will use methods in a class to modify them.

In a way, you could say that an object is actually a class in an object-oriented language such as C#.