Main concepts in OOP

后端 未结 14 2168
夕颜
夕颜 2020-12-07 17:03

I was once asked in an interview \'What are the 3 main concepts of OOP?\'. I answered by saying that in my opinion there were 4 which are as follows:

  • Inheritan
14条回答
  •  渐次进展
    2020-12-07 17:21

    The four pillars are as your correctly state

    • Encapsulation.
    • Abstraction
    • Inheritance
    • Polymorphism

    Encapsulation deals with containing data, nothing more, nothing less.

    Abstraction deals with data abstraction, i.e. is all this data really relevant. Think of a bank which contains information on name, age, address, eye colour, favourite tie, etc. Are eye colour and favourite tie really that relevant to the banks requirements? No. This is abstraction.

    Inheritance deals with generalisation. Information which can apply to more than one thing. If something inherits from something then it can be said to be a more specific type of that thing. For example, Animal. A Dog is a type of Animal, so Dog inherits from Animal. Jack Russell is a type of Dog, so Jack Russell inherits from Dog.

    Polymorphism deals with things having multiple forms, (poly - morph). Two kinds in programming,

    • Late Binding,
    • You refer to something as it's general type and hence the compiler does not know what to bind at compile time. Think method Overriding.

    • Early Binding

    • You redefine a method using a different signature, i.e. int add(int a, int b) vs double add(double a, double b)

    These are essentially the basic principles of Object Orientation. There is a lot of overlap between these and so it is quite important to achieve a clear understanding of what each of these mean.

提交回复
热议问题