Object-Oriented Programming Concepts “In Simple English”

Intro to C#

In this article, we are going to explore c#, an OOP language, in order to showcase and understand OOP concepts.

What is C#?

C# is a simple, modern, general-purpose, object-oriented programming language developed by Microsoft within its .NET initiative led by Anders Hejlsberg. C# is a modern, general-purpose, object-oriented programming language developed by Microsoft and approved by the European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO). C# is based on object-oriented programming concepts.

The following reasons make C# a widely used professional language:

  • It is a modern, general-purpose programming language
  • It is object-oriented.
  • It is component oriented.
  • It is easy to learn.
  • It is a structured language.
  • It produces efficient programs.
  • It can be compiled on a variety of computer platforms.
  • It is a part of .Net Framework.

What is The .Net Framework?

The .Net framework is a revolutionary platform that helps you to write the following types of applications:

  • Windows applications
  • Web applications
  • Web services

The .Net framework applications are multi-platform applications. The framework has been designed in such a way that it can be used from any of the following languages: C#, C++, Visual Basic, JScript, COBOL, etc. All these languages can access the framework as well as communicate with each other.

The .Net framework consists of an enormous library of codes used by client languages such as C#. Following are some of the components of the .Net framework:

  • Common Language Runtime (CLR)
  • The .Net Framework Class Library
  • Common Language Specification
  • Common Type System
  • Metadata and Assemblies
  • Windows Forms
  • ASP.Net and ASP.Net AJAX
  • ADO.Net
  • Windows Workflow Foundation (WF)
  • Windows Presentation Foundation
  • Windows Communication Foundation (WCF)
  • LINQ

A C# program consists of the following parts:

  • Namespace declaration
  • A class
  • Class methods
  • Class attributes
  • A Main method
  • Statements and Expressions
  • Comments

Here is a small program of C#:

Let us look at the various parts of the given program:

  • The first line of the program using System; — the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
  • The next line has the namespace declaration. A namespace is a collection of classes. The “HelloWorldApplication” namespace contains the class HelloWorld.
  • The next line has a class declaration, the class “HelloWorld” contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.
  • The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.
  • The next line /*…*/ is ignored by the compiler and it is put to add comments in the program.
  • The Main method specifies its behavior with the statement Console.WriteLine(“Hello World”);
“WriteLine” is a method of the Console class defined in the System namespace. This statement causes the message “Hello, World!” to be displayed on the screen.

It is worth to note the following points:

  • C# is case sensitive.
  • All statements and expression must end with a semicolon (;).
  • The program execution starts at the Main method.

OOP Concepts

In the class-based object-oriented programming paradigm, “object” refers to a particular instance of a class where the object can be a combination of variables, functions, and data structures. A good understanding of OOPs concepts can help in decision making when designing an application. How you should design an application and what language should be used.

Object Oriented programming is a programming style which is associated with the concepts like class, object, Inheritance, Encapsulation, Abstraction, Polymorphism.

1. Class

A class is a collection of method and variables. It is a blueprint that defines the data and behavior of a type.

Let’s take Human Being as a class. A class is a blueprint for any functional entity which defines its properties and its functions. Like Human Being, having body parts, performing various actions.

We can define a class using the class keyword and the class body enclosed by a pair of curly braces, as shown in the following example:

public class humanBeing
{
// declare field properties, event, delegate, and method
}

2. Inheritance

Inheritance is a feature of object-oriented programming that allows code reusability when a class includes property of another class. Considering HumanBeing a class, which has properties like hands, legs, eyes, mouth, etc, and functions like walk, talk, eat, see etc.

Man and Woman are also classes, but most of the properties and functions are included in HumanBeing. Hence, they can inherit everything from class HumanBeing using the concept of Inheritance. Here is a code example:

Inheritance code example

Output:

Calling the human being constructor
I'm a man, a male human being
I'm a human being

3. Objects

My name is Yann, and I am an instance/object of class Man. When we say, Human Being, Man or Woman, we just mean a kind, you, your friend, and I. We are the forms of these classes. We have a physical existence while a class is just a logical definition. We are the objects.

The syntax for creating an object of a class Man:

Man Yann = new Man();

4. Abstraction

Abstraction means, showcasing only the required things to the outside world while hiding the details. Continuing our example, Human Being’s can talk, walk, hear, eat, but the details of the muscles mechanism and their connections to the brain are hidden from the outside world.

The concept of abstraction focuses on what an object does, instated of how an object is represented or “how it works.” Thus, data abstraction is often used for managing large and complex programs.

5. Encapsulation

Encapsulation means that we want to hide unnecessary details from the user. For example, when we call from our mobile phone, we select the number and press call button. But the entire process of calling or what happens from the moment we press or touch the call button to the moment we start having a phone conversation is hidden from us.

6. Polymorphism

Polymorphism is a concept, which allows us to redefine the way something works, by either changing how it is done or by changing the parts used to get it done. This can be done in two ways, overloading and overriding.

If we walk using our hands, and not legs, here we will change the parts used to perform something. Hence this is called Overloading.
Polymorphism: Early binding or Overloading code example

Output:

10
15
And if there is a defined way of walking, but I wish to walk differently, but using my legs, like everyone else. Then I can walk like I want, this will be called as Overriding.
Polymorphism: Late binding or Overriding code example

Output:

Hello! Nice to meet you!
Cheers!!!