views:

1959

answers:

13

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:

  • Inheritance
  • Encapsulation
  • Abstraction
  • Polymorphism

Was I correct?

+2  A: 

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.

Mitchel Sellers
I would find it hard to leave out "abstraction". That's part of the thrust behind the "new" Domain-Driven Design... to create abstractions that model the world as closely as possible.
Mark A Johnson
I agree with Mitchel, I often hear people lump encapsulation and abstraction in the same boat.
Kevin
@Mark - I agree, as an instructor I stress the four, but sadly not all people put emphasis on abstraction, some see it as "just there.
Mitchel Sellers
I guess it depends how and when you learned to program. I programmed in BASIC, assmebler, Pascal, FORTH, and C long before going anywhere near OOP, and good abstraction was always a design goal. The other three aspects of OOP simply combine to allow much better abstraction.
Shane MacLaughlin
+3  A: 

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

duffymo
Or its a trick question :P
Ólafur Waage
Indeed. There are really \pi main concepts - I never get the fractional one right.
duffymo
+12  A: 

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.

Shane MacLaughlin
Abstraction is in my opinion the most important part of them all.
Filip Ekberg
I was also taught Encapsulation, Inheritence and polymorphism. I know what Abstraction is, of course, but how does "abstraction" apply any more to OOP than to procedural languages?
Charles Bretana
@Filip, I agree it is the most important part of much programming but would ague that it is unique to OOP. Once you can create your own functions and procedures that are solving high level domain specific tasks, you are by definition abstracting.
Shane MacLaughlin
I agree: saying that abstraction is a principle of OOP is like saying that Turing completeness is a principle of OOP. It's true, of course, but if necessary you could let it go without saying and not do any harm.
Steve Jessop
What kind of programming can you do *without* abstraction?
Apocalisp
Unmaintainable programming. For instance, applying a genetic algorithm to produce a neural net that solves the problem. The resulting net (that is, program) typically contains no identifiable abstractions.
Steve Jessop
You can do quite a bit of programming without creating abstractions by yourself (typing shell command for example), although you will rely on the many abstractions your programming environment provides (except when you program in machine language).
Rene Saarsoo
@Rene, if you type your shell commands into a file to form a named batch process to perform a specific task that you regularly carry out, that is a level of abstraction. Most of my early programming was in z80 assembler (machine code), and I regularly abstracted common functions into single calls.
Shane MacLaughlin
Abstraction was one of the guiding principles behind the older "Structured Programming", so I'm with you on this one.
T.E.D.
Abstraction is a general concept; encapsulation et al are kinds of abstraction, just as a subroutine is a kind of abstraction. See http://en.wikipedia.org/wiki/Abstraction_(computer_science)
Steven A. Lowe
Abstraction is omnipresent in computer science.
Jason
A: 

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

frankodwyer
+2  A: 

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.

Mark A Johnson
A: 

You are indeed correct. Here's a reference for ya: http://docs.rinet.ru/KofeynyyPrimer/ch4.htm

Filip Ekberg
A: 

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.

Frederick
+3  A: 

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.

Konrad Rudolph
templates emulate OOP; they provide concise syntax, but they do not reuse code. Instead they expand and bloat the code ;-)
Steven A. Lowe
what you describe, in fact, is called Static Polymorphism in "C++ Templates The Complete Guide".
Comptrol
@Comptrol: no, this is something else. Template subclassing establishes an inheritance hierarchy through base classes and has in fact several additional properties (code reuse) of OOP. Static polymorphism doesn't have this, it's merely an ad-hoc relationship between a priori unrelated types.
Konrad Rudolph
A: 
A: 

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
Cheery
Late binding?????
erikkallen
It was the main feature of smalltalk, and main feature of the kind of OOP smalltalk provided.
Cheery
+12  A: 

There are 3 requirements for a language to be object-oriented:

  • a language that supports only encapsulation (objects) is not object-oriented, but it is modular
  • a language that supports just encapsulation (objects) and message-passing (polymorphism) is not object-oriented, but it is object-based
  • a language that supports encapsulation (objects), message-passing (polymorphism), and inheritance (abstraction), is 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

Steven A. Lowe
A good answer, certainly got my vote. You could maybe improve it by adding a couple of examples for each of the above.
Shane MacLaughlin
These don't make up an object oriented language, a language having all those things is just a language that supports object orientation. A really object oriented language is one where these things are build deeply into the language.
Rene Saarsoo
@[Rene Saarsoo]: define the difference please.
Steven A. Lowe
Yep, there is nothing most programming languages to prevent you from writing huge unstructure monolithic lumps of code. Worse still, some people do ;)
Shane MacLaughlin
A: 

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.

A: 

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.

Apocalisp