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