Reputation: 19407
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:
Was I correct?
Upvotes: 28
Views: 15419
Reputation: 223
OOP
Abstraction (ignoring or hiding details that don't matter) - the situation in which a subject is very general and not based on real situations.
Encapsulation - Keeping properties and methods private onside the class, so they not accessible from outside the class.
! API - is essentially all the methods that are not private, not encapsulated.
Inheritance - Making all properties and methods of a certain class available to child class.
Polymorphism-gr: "many shapes" - a child class can overwrite a method it inherited from a parent class.
Upvotes: 1
Reputation: 21742
It's generally believed that those are the main principles however they had very little to do with why OO was created.
One of the guiding principles was the direct manipulation metaphor. That is creating an object in the program that represented an object from the users mental model. Most of the motivation for creating OO was based in psychology not math/CS as is often believe to be the case these days.
If doubtfull of this take a look at some of the work by Trygve Renskauge. Father of MVC and DCI or James Coplien accomplish author and speaker.
So I'd say you likely gave them an answer close to what they expected whether it's correct depends on where you stand.
Upvotes: 0
Reputation: 61
The four pillars are as your correctly state
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,
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
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.
Upvotes: 6
Reputation: 61233
There are 3 requirements for a language to be object-oriented:
NOTE: Abstraction is a much more general concept; encapsulation et al are kinds of abstraction, just as a subroutine is a kind of abstraction. See Abstraction
Upvotes: 55
Reputation: 63126
Most people would consider that correct, my guess is if they were asking for three it would be Inheritance, Encapsulation and Polymorphism.
I personally find that those three concepts are the real "meat" if you will behind the definition of OOP. And most people take abstraction for granted and lum it in with the others, as really it could be considered part of any of the other three.
When I talk about OOP though I always mention the 4.
Upvotes: 3
Reputation: 35054
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.
Upvotes: 1
Reputation:
It could have been a trick question for the interview, but in Computer Science classes these days they teach the 4 Pillars of Object Oriented Programming.
Upvotes: 0
Reputation: 25423
3 main concepts in OOP:
Upvotes: 1
Reputation: 765
This [article][1] refers to the three pillars of good code. I found it to be an excellent article positing that encapsulation is the "first principle" of object-oriented design.
"First" principles are fundamental, underlying principles from which all else springs. The author uses the example of the Golden Rule. It's difficult to teach children all the finer points of civilized behavior but if you can get them to understand (and more importantly, practice) the Golden Rule of treating others as you would like to be treated, then they are more likely to "get" all the legal and moral standards we're held to on a daily basis.
So, it follows that if a developer understands encapsulation as a "First Principle" of object-oriented development, all of the other principles will follow in due course.
I don't do the author's content justice but I would definitely encourage people to read it.
For some reason I'm not showing the hyperlink as coming through so here's the URL: http://www.netobjectives.com/files/Encapsulation_First_Principle_Object_Oriented_Design.pdf
Upvotes: 0
Reputation: 545588
The problem with OOP is that nobody bothered to give a proper, concise, agreed-upon definition. Especially, I'd like to point out that all the aspects you mentioned can well be put into action without the use of object orientation!
Two type systems that do this are the Haskell type system, which, by consense, is generally not regarded to be object-oriented, and C++ templates with template subclassing. However, it could perhaps be argued that template subclassing emulates OOP.
Since template subclassing is not a widely known mechanism, let me give an example from the SeqAn library where it was invented.
String<Char> cstr = "This is a test";
String<Dna, Packed<> > dstr = "GATTACA";
cout << "length(" << cstr << ") = " << length(cstr) << endl;
cout << "length(" << dstr << ") = " << length(dstr) << endl;
Here, String<Char>
and String<Dna, Packed<> >
are inherited of the “abstract class” String<>
. They encapsulate the concept of a string, using completely different methods. They share the polymorphic length
method, implemented differently for both concrete types.
Upvotes: 5
Reputation: 36838
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.
Upvotes: 1
Reputation: 958
Yes, those are the standard four.
Some people combine abstraction and encapsulation. I'm not sure why... they're not completely orthogonal, but maybe there's enough overlap? There's certainly overlap between inheritance and polymorphism, but it would be hard to combine them, in my mind.
Upvotes: 3
Reputation: 14048
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'.
Upvotes: 1
Reputation: 22922
I would say that abstraction is not solely an OOP concept, in that you can abstract to a large degree in many non-OOP languages.
Upvotes: 13
Reputation: 308763
Those are the Four Horsemen as I know them. Maybe they mistakenly lump Inheritance and Polymorphism together.
Upvotes: 4