C# | OOP Principles | Simplified

Object Oriented Programming, is a paradigm of programing in which a program is built around objects in which they communicate/depend/ extend each other.

OOP is the paradigm that is mostly used in the industry and any programmer in this day and age is practicing the design.

The most popular way OOP is implemented by class based structure, where classes are representations of Entities in the real world
(Car, Person, Location etc..)

None the less, there are sets of principles/guidelines that good to keep in mind to have a healthy usage of this programming paradigm.

— Principles of Object-Oriented Programming

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

1-Encapsulation

Encapsulation is the practice of hiding/concealing the internal implementation of a class that should not be exposed throughout the program but needed to be referenced in the class internal implementation, this practice offers less volatility to the class properties and in exchange methods can be used to interface with the class.

  • Example

In here, have a Person class that has couple of public Properties and private ID, since ID is a sensitive part of Person Information, we set it as private and have a public property that retrieves the ID with a masked value using MaskID method. this way we do not expose the actual value of the ID and we can easily change the representation of ID if when it’s retrieved.

2-Abstraction

Abstraction is an extension of encapsulation. It means providing only the necessary information to the outside world while hiding the internal details of implementation. It reveals only the appropriate operations for other objects. The advantage of this is that we can change the implementation without affecting the class, as the method interface remains the same.

  • Example

Looking at the Person class example above, we can make the class abstract and have many other classes derive from it (Inheritance) in such a way that we can override (Polymorphism) the methods/ implementations of the base class

So, if we have a Professor class, that class can Inherit from Person and have all of it’s properties. this helps us lower down the code writing time and resulting into much more concise clean code.

3-Inheritance.

In inheritance, we create a new class called the child class, which is derived from the class called the parent class, thus forming a hierarchy of classes. The child class reuses the data fields and methods required from the parent class and implements its unique functionality on its own.

  • Example

— Professor class

4-Polymorphism

Is the ability of overriding base/abstract class method implementations that might need to perform a different operation on a specific derived class

  • Example

I made the GetFullName virtual so it can be overridden in the Professor class, having the virtual keyword allows us to override the methods' internal implementation so we can have custom operation when we need it on a derived class.

Now, every time we instantiate a new object of professor the GetFullName will return a different value than the Person class, and that’s the power of Polymorphism, where we can change the internal implementation of a base class method to meet our business needs and use cases easily and without rebuilding a whole class form the ground up, instead we used all the Person class properties and simply override the GetFullName method to have a different return value.

-Reflection

OOP is an interesting and helpful paradigm, it absolutely eases the process of development and shortcuts lots of boiler plate code as we’ve seen in the examples of Polymorphism and Inheritance.

There key points we need to take into consideration when dealing with this way of development.

— Advantages

  • A real-world idea can be demonstrated, as everything in OOP is treated as an object.
  • As we use the concept of encapsulation, programs are easier to test and maintain.
  • Faster development of code is done as we develop classes parallel instead of sequentially.
  • OOP provides greater security due to data abstraction. The outside world cannot access the hidden data.
  • Reusability can be achieved by using classes that have been already written.

— Disadvantages

  • Designing a program with an OOP concept can be tricky.
  • A programmer needs to plan beforehand for developing a program in OOP.
  • The size of programs developed with OOP is bigger than those developed with a procedural approach.
  • Since OOP programs are larger in size, the execution time for these programs is also more.

Science is what we understand well enough to explain to a Computer. Art is everything else we DO!
~
Donald Knuth

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ibrahim Jaber

Ibrahim Jaber

Software developer | Programming and Blockchain enthusiast