What is OOP?

Mustafa Berk ÇERDİK
baakademi

--

Hello everybody,

I would like to mention about OOP, it is the most important topic for software developing and also developers. Nowaday every developer has to know OOP and they should keep coding according to Object Oriented
Programming basics.

Well, I highly tend to talk about OOP basics in general and also I want to explain somethings with example in this article. If you have coffee for enjoying this writing, we could start to talk OOP.

C# provides full support for object-oriented programming including abstraction, encapsulation, inheritance, and polymorphism.

  • Abstraction means hiding the unnecessary details from type consumers.
  • Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.
  • Inheritance describes the ability to create new classes based on an existing class.
  • Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.
OPP Structure 1

Access Specifiers

C# provides four types of access modifiers: private, public, protected, internal, and two combinations: protected-internal and private-protected.

public:Objects that implement public access modifiers are accessible from everywhere in our project. Therefore, there are no accessibility restrictions.

private:Objects that implement private access modifier are accessible only inside a class or a structure. As a result, we can’t access them outside the class they are created.

protected:The protected keyword implies that the object is accessible inside the class and in all classes that derive from that class.

internal:The internal keyword specifies that the object is accessible only inside its own assembly but not in other assemblies.

protected internal:The protected internal access modifier is a combination of protected and internal. As a result, we can access the protected internal member only in the same assembly or in a derived class in other assemblies.

private protected:The private protected access modifier is a combination of the private and protected keywords. We can access members inside the containing class or in a class that derives from a containing class, but only in the same assembly(project). Therefore, if we try to access it from another assembly, we will get an error.

Classes and Objects

Classes and objects are the main power of OOP. If someone know has to command classes and objects he/she can develop their own project easily and simply.

Let’s define a class ;

class SampleForWriting{
//some properties and methods
}

Class Members

Each class can have different class members that include properties, methods, even class and events that provide communication between different classes and objects.

Properties and Fields

Fields and properties represent information that an object contains. Fields are like variables because they can be read or set directly, subject to applicable access modifiers.

class Cars{
public int CarName{ get; set; }
}
class SampleClass
{
private int _sample;
public int Sample{
// Return the value stored in a field.
get => _sample;
// Store the value in the field.
set => _sample = value;
}
}

Abstraction

Abstraction is “To represent the essential feature without representing the background details.”

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or objects by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.

Suppose you have 3 mobile phones as in the following:

Nokia 1400 (Features: Calling, SMS)

Nokia 2700 (Features: Calling, SMS, FM Radio, MP3, Camera)

Black Berry (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

So that, for a mobile phone object you will have the abstract class as in the following,

abstract class MobilePhone{
public void Calling();
public void SendSMS();
}
public class Nokia1400:MobilePhone {}
public class Nokia2700:MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
}
public class Nokia1400:MobilePhone {
public void FMRadio();
public void MP3();
public void Camera();
public void Recording();
public void ReadAndSendEmails();
}

Abstraction means putting all the variables and methods in a class that are necessary.

Intheritance

Inheritance enables you to create a new class that reuses, extends, and modifies the behavior that is defined in another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. However, all classes in C# implicitly inherit from the Object class that supports .NET class hierarchy and provides low-level services to all classes.

Example For Intheritance

As you can see in the picture right above, we have 3 abstract classes and also their own concrete object which instance from abstract classes.Each class has properties about thmeselves and each object inherit from their own class.

As you can see every phone models defined from MobilePhone class, like

public class Nokia1400:MobilePhone {}
public class Nokia2700:MobilePhone {}
public class Nokia1400:MobilePhone {}

Also if we need to ‘ How to define example picture?’ ,like

public class EmpireState:Building {}
public class Lassie:Dog {}
public class YourComputer:ComputSS
OOP Structure 2

Interfaces

Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. An interface represents a contract, in that a class that implements an interface must implement every aspect of that interface exactly as it is defined.

To define an interface:

interface ISampleInterface{
void DoSomething();
}

Constructors

Constructors are class methods that are executed automatically when an object of a given type is created. Constructors usually initialize the data members of the new object. A constructor can run only once when a class is created. Furthermore, the code in the constructor always runs before any other code in a class. However, you can create multiple constructor overloads in the same way as for any other method.

Encapsulation in Object Oriented Programming

Hiding internal state and requiring all interaction to be performed through an object’s methods is known as data encapsulation — a fundamental principle of object-oriented programming.

The encapsulation is the process of grouping or wrapping up of data and functions to perform actions on the data into the single unit. The single unit is called a class. Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object. It keeps the data and the code safe from external interference.

The main purpose or use of the encapsulation is to provide the security to the data of a class.

To make the data secure we need to use private access modifiers that will restrict the access to the data outside the class. The access modifiers are used to define the access level or scope of the class members like data members and functions.

References:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/object-oriented-programming#

https://www.c-sharpcorner.com/UploadFile/mkagrahari/introducti:n-to-object-oriented-programming-concepts-in-C-Sharp/

https://www.topperskills.com/tutorials/oop/object-oriented-programming-encapsulation-concepts.html

https://code-maze.com/csharp-basics-access-modifiers/#:~:text=C%23%20provides%20four%20types%20of,%2Dinternal%20and%20private%2Dprotected.

--

--