OOP Four Principles Explained In Five Minutes
If you can’t explain it simply, you don’t understand it well enough
At the start of my career as a junior engineer, I was asked to explain the four principles of OOP (Object-Oriented Programming) at almost every company I’ve interviewed at. I used to get nervous while answering these questions. It wasn’t because I didn’t know the answer, but rather because there were so many variations of the same answer. I am talking about Encapsulation, Abstraction, Inheritance, and Polymorphism. Now and then, I find myself engaging in conversation related to these principles and found out there are still more experienced developers who understand the principles but don’t always know how to explain them to younger engineers. I’m going to do my best to give a thorough but simple explanation of each principle.
Encapsulation
Encapsulation simply means to only allow changes to a class private members, which includes properties and methods, via public methods. No direct access to a class property fields should be allowed.
Consider our Student class code snippet below
public class Student
{
public string FirstName;
public string LastName;
public int Age;
public string Id;
}