Main concepts in OOP

后端 未结 14 2112
夕颜
夕颜 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:19

    A proper answer to the question is: "Please clarify what you mean by Object-Oriented Programming." Oops, that would be telling, because the real question being asked is: "When I say OOP, what do I mean?"

    There's no correct answer.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 17:22

    3 main concepts in OOP:

    • Late binding
    • Concept reusing (not sure about this, anyway: re-using of concepts; avoiding the re-implementation of even the simplest concepts)
    • Abstraction
    0 讨论(0)
  • 2020-12-07 17:23

    Those are the Four Horsemen as I know them. Maybe they mistakenly lump Inheritance and Polymorphism together.

    0 讨论(0)
  • 2020-12-07 17:25

    That's correct.

    If you had to provide only one, however, Abstraction it's got to be, for, one way or the other, the rest three is merely Abstraction in action.

    0 讨论(0)
  • 2020-12-07 17:28

    Probably the last three is what they were looking for - inheritance could be argued to be more of a mechanism to help achieve the others, which are higher level goals.

    There is not really a correct answer anyway, especially if limited to 'top 3'.

    0 讨论(0)
提交回复
热议问题