C# Tutorial – Part 3: Object-Oriented Principles

After knowing some C# syntaxes, method, and class, it’s a time to learn more about Object-Oriented Principles (OOP). Thus, I will cover most of the things about OOP in this #3 tutorial.

What will cover in this tutorial?

  1. OOP Goals
  2. Abstraction, Encapsulation, Inheritance, Polymorphism

What are OOP Goals?

Before understanding what OOP is, you need to understand why you need to use OOP for developing software. Thus, I put this question at the top of this post. There are 4 reasons why you need to use OOP for your program.

  • Robustness: When developing software, we want our programs to be able to handle unexpected inputs that are not explicitly defined inside the system. Imagine that you’re developing an app that uses for calculating some basic maths and there are many numeric data types – integer, float, double, long, etc. This leads to the fact that you will need to develop a function/method that has the capability of handling all of the numeric data types. How can we do that? we can do that by using the overloaded function/method concept of the polymorphism concept of OOP – We will dig into this concept later in this post.
  • AdaptabilityWe also want our programs to be able to evolve over time in response to changing conditions in its environment. Imagine that you’re developing a sum function/method for your calculating app. This function can handle 2 numbers with the same data type. After finishing the development part, you’re going to test your program and you accidentally give the sum function 2 numbers which have different data types from each other – float and integer. As a result, your program raises the error and you need to fix your code to adapt to any changing conditions. This is what we call adaptability.
  • Portability: Going along with the adaptability concept is the portability concept. Unlike adaptability, if your calculating app is portable, it can run with minimal change on different hardware and operating system platforms. Let’s say you’re developing your program in Windows operating system. To achieve portability, your program needs to be able to run on macOS as well as any other Linux operating system.
  • Reusability: To make your program efficient, you will need to minimize the complexity of your code – too many lines, duplicated functions/methods, etc. – and make your code reusable. It means that you can use one function/method many times and anywhere without duplicating that function/method when using it. Imagine that your calculating app has a LinearRegression class – for calculating the linearity of two or more variables/elements – and a NonLinearRegression class. Both classes need to use the sum function that you have developed above. However, the sum function is put in the Utility class which is a different class from two classes above. To achieve reusability, your program needs to be able to call the sum function/method in two classes above without rewriting the function in those classes.

Abstraction, Encapsulation, Inheritance, and Polymorphism:

To understand OOP, we need to understand 4 important concepts around it.

  • AbstractionIt is about hiding unnecessary details while giving out the most essential details. The main goal of abstraction is to reduce the complexity of the system down to its most fundamental parts and describe these parts in a simple language. Imagine that the user is using your calculating app for summing 2 numbers. He/she doesn’t need to know how you construct the sum function in your app because it will be hidden to him/her. The only thing he/she can do is giving 2 numbers with the ‘+’ operator and clicking on the  ‘=’  button. Then, the program will run and generate the result. That’s the beauty of abstraction. It hides what the user doesn’t need to know and show what the user wants to know.
  • EncapsulationIt states that different components of a software should not reveal the internal details of their respective implementations. In other words, encapsulation is how you hide the implementation code and data into a single class or method to protect the inner working of an object from the outside world. Going back to the example of the LinearRegression class and the NonLinearRegression class above, these classes will need to use the sum function/method in the Utility class. However, they don’t need to know everything inside the Utility class, excepting the sum function/method with its parameters. In this case, the Utility class hides unnecessary details to both classes above and it makes the sum function/method public to every class. That’s the beauty of encapsulation. Unlike abstraction – hiding unnecessary details to the user, it hides unnecessary details between classes so that they can communicate well to each other and reduce the complexity of the system.
  •  InheritanceThis allows the design of generic classes that can be specialized to more particular classes, with the specialized classes reusing the code from the generic class. In other words, you have 2 classes and one of them acquires another one, and hence you have 1 parent class and 1 child class from 2 classes if you apply the inheritance concept. Normally, a child class inherits a parent class if and only if a child class is a kind of a parent class. For example, the sum function/method in the Utility class of your calculating is a kind of a Math class. As a result, the Utility class inherits the Math class – the Math class is a parent class of the Utility class. There are 3 important phrases in the inheritance concept.
    • Generalization: This is a phrase that describes how the essential characteristics (variables/elements, properties or methods) from two or more child classes are extracted and combined inside a parent class.
    • Specialization: This is a phrase that describes how a child class is created from a parent class.
    • Extension: This is a phrase that describes how a child class extends its functionality after inheriting from a parent class. For example, your Math class doesn’t have a sum function/method but its child class – the Utility class – does have one as well as all characteristics from the Math class.
  • PolymorphismThis allows the function/method in a particular class to take many different types. Going back to the sum function/method with a parameter which contains two different types – float and integer, you want to return a float value after summing those two elements/variables. However, you also want to return an integer value after summing two integer elements/variables. To achieve that, you need to use the concept of polymorphism because it allows you to create 2 or more functions/methods with the same name and different types. There are also 2 important phrases in the polymorphism concept.
    • Specialization: Unlike the one in the inheritance concept, it helps a particular child class to override the existing function/method in its parent class. Importantly, a particular child class needs to inherit its parent class in order to apply the overriding method concept. We’ll go deeper into this concept with the example in the next tutorial.
    • Extension: Unlike the one in the inheritance concept, it helps a particular class to overload the existing function/method inside that class. Importantly, we don’t need to apply the inheritance concept here. Also, we’ll go deeper into this concept with the example in the next tutorial.

These are most of the essentials concepts you need to know about Object-Oriented Principles. There is no coding example in this tutorial because I will cover it in the next tutorial – Object-Oriented Programming. Until I publish the next tutorial, I encourage you to learn more on Google and go a bit deeper into all the concepts above.

Hopefully, this #3 tutorial helps you make more sense about Object-Oriented PrinciplesI will publish the #4 tutorial soon. If you have any questions or any topics you want to learn more, please let me know.

Thank you for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s