I love coding, coding everyday is the dream! This is one of my projects built with ASP.NET MVC, C# and Postgres to share everything I'm learning on my coding journey. Join me and checkout my portfolio to see more of my projects.
Object Oriented Programming (OOP) is one way to write code. It is centered around objects which contain variables (properties) and functions (methods). C# is an example of an OOP language. There are 4 main pillars that come supported with any OOP language which are: Abstraction, Polymorphism, Inheritance, and Encapsulation. Use the acronym "A PIE" to help remember. These pillars need to be understood in order to write great code.
Abstraction
Abstraction is when only the relevant details are exposed while the rest is hidden. It can be achieved with either abstract classes or interfaces.
For example, in my bug tracker, the function of the database is abstracted away through the use of interfaces. Only service methods defined in the interface can be used to call upon the database.
Polymorphism
Polymorphism allows an object to have many forms. Objects can be processed in different ways depending on the data type or class. There are two main types of polymorphism: compile-time and runtime.
Compile-time Polymorphism
Compile-time polymorphism is when there are multiple methods with the same name but different signatures all contained within the same class. This is also known as method overloading.
Runtime Polymorphism
Runtime polymorphism is when a derived class is implementing a method with the same signature as the base class. The method in the base class needs to have the “virtual” keyword and the method in the derived class needs to have the “override” keyword. This is also known as method overriding.
Inheritance
Inheritance is a way to reuse code between at least two classes. The derived class is able to use the same properties and methods that the base class implements and then add on some of its own.
For example in my bug tracker, I use inheritance to extend the IdentityUser class. The derived class is called BTUser and I added additional properties such as First Name and Last Name. After extending IdentityUser, I'm able to replace all appearances of IdentityUser in my application with BTUser which is also an example of the Liskov Substitution Principle from the SOLID principles.
Encapsulation
Encapsulation bundles data and the methods that operate on the data in a single class. This allows for data hiding where direct access to data is hidden. So the way that the data is accessed is controlled.
0 comments