Best way to explain OOP Principles? [closed]

2020-05-24 16:47发布

   1. Abstraction
   2. Encapsulation
   3. Inheritance 
   4. Polymorphism 

How to best explain these concepts, for example, in an interview?

3条回答
该账号已被封号
2楼-- · 2020-05-24 17:11

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...

查看更多
手持菜刀,她持情操
3楼-- · 2020-05-24 17:15

1. Abstraction

Show only necessary thing to user that he required, not extra information (use public private, protected). Abstraction is done when we need to inherit from certain class but do not instantiate the objects of that class.

2. Encapsulation

Group all relevant things together. I.e. encapsulation is wrapping/binding up of data and member functions in single unit. In simple, abstraction is hiding the implementation and encapsulation is to hide data.

3. Inheritance

If something already exist, why should I recreate it (same as re-inventing a wheel). Use inheritance to inherit all things of that class into your class. Inheritance enables you to create new classes that re-use, extend and modify the behaviour that is defined in other classes

4. Polymorphism

When an object exhibits different behavior in different situation. In simple way, when a message can be processed in different ways/forms.

查看更多
▲ chillily
4楼-- · 2020-05-24 17:20

When speaking with non-technical people, I use analogies.

  1. 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.

  2. 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.

  3. 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".

  4. 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.

查看更多
登录 后发表回答