1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
How to best explain these concepts, for example, in an interview?
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
How to best explain these concepts, for example, in an interview?
car blueprint -> class
car -> object
wheel -> member
police car blueprint -> child class
car engine -> encapsulated (users of your car object dont need to interact with it, better hide it)
go fast from a to b -> polymorphic method , can be called on regular car or police car, but won't do the same thing (police car will switch siren on and burn lights)
need to describe how those cars can be used ? Create IVehicle which is their interface. No such thing as IVehicle exists in the real world, but it's useful anyway for instantly describing things that implement it. -> abstraction.
etc...
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
When speaking with non-technical people, I use analogies.
Abstraction
It is like programming the shared behavior of a Lion, a Pinguin and a Salmon. All of them are different, but they share some traits: they all eat, breath, die, etc. That is why abstraction is important, because it allows me as developer to program something like "Animal" object, that defines their common behavior.
Encapsulation
Imagine a big company with multiple department.
Each department offers services to people, like "customer service calls": public methods.
Then, departments interact with each other, request information, delegate tasks: protected methods.
And finally, some duties are managed internally in each department, for example, pays their employees: private methods.
Thus, encapsulation would be to set some services as public, protected and private depending on who can request them.
Inheritance
Remember those lions, pinguins and salmons? And remember that they all are animals? Well, inheritance would be, for example, if when an animal is born their "birth date" is define as "today". That behavior would be shared by all animal types and therefore, lions pinguins and salmons would inheritate the "born behavior".
Polymorphism
This one is pretty similar to inheritance, being the major difference a technical difference, and therefore I would not even try to explain it to a non-technical person.