28

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?

Peanut
  • 18,967
  • 20
  • 72
  • 78

15 Answers15

55

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
  • 60,273
  • 18
  • 132
  • 202
  • 2
    A good answer, certainly got my vote. You could maybe improve it by adding a couple of examples for each of the above. – SmacL Dec 31 '08 at 14:39
  • 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 Dec 31 '08 at 15:16
  • @[Rene Saarsoo]: define the difference please. – Steven A. Lowe Dec 31 '08 at 15:20
  • Yep, there is nothing most programming languages to prevent you from writing huge unstructure monolithic lumps of code. Worse still, some people do ;) – SmacL Dec 31 '08 at 15:45
  • How polymorphism is equal to message-passing – XMen Jan 05 '11 at 12:30
  • @Rahul: message-passing enables polymorphism – Steven A. Lowe Jan 05 '11 at 16:28
  • C does actually support all three but that doesn't make it an OO language. I think the definition would serve well at interviews though – Rune FS Sep 28 '11 at 07:26
  • @Rune FS: C does not have inheritance or message-passing. You're thinking of C++ – Steven A. Lowe Sep 28 '11 at 15:24
  • Nope I'm not thinking C++ but C. pointers, function pointers and structs is basically all you need to accomplish your three bullets all supported in C – Rune FS Sep 28 '11 at 15:44
  • @Rune FS: to implement it yes - but that's not the same as native language support. I have written OO languages in C, but C by itself is not OO – Steven A. Lowe Sep 28 '11 at 16:09
  • That's what I'm saying. I can have all of the above in C but it's not an OO language. With implement I didn't me an compiler/interpreter but use all of the above in my implementation of a program – Rune FS Sep 28 '11 at 16:34
  • @RuneFS: you can have all of that in Assembly, but it's not an OO language either. ;-) – Steven A. Lowe Sep 28 '11 at 18:05
  • And that's my point. Though your list describes an OO language it is not limited to OO languages. Non OO languages fits in too – Rune FS Sep 29 '11 at 05:03
13

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.

SmacL
  • 22,555
  • 12
  • 95
  • 149
  • Abstraction is in my opinion the most important part of them all. – Filip Ekberg Dec 31 '08 at 13:54
  • 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 Dec 31 '08 at 14:22
  • @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. – SmacL Dec 31 '08 at 14:33
  • 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 Dec 31 '08 at 14:41
  • 2
    What kind of programming can you do *without* abstraction? – Apocalisp Dec 31 '08 at 14:41
  • 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 Dec 31 '08 at 14:51
  • 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 Dec 31 '08 at 15:03
  • @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. – SmacL Dec 31 '08 at 15:09
  • Abstraction was one of the guiding principles behind the older "Structured Programming", so I'm with you on this one. – T.E.D. Dec 31 '08 at 15:10
  • 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 Dec 31 '08 at 15:15
  • Abstraction is omnipresent in computer science. – jason Dec 31 '08 at 16:08
6

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.

Kipper
  • 61
  • 1
  • 1
5

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
  • 530,221
  • 131
  • 937
  • 1,214
  • templates emulate OOP; they provide concise syntax, but they do not reuse code. Instead they expand and bloat the code ;-) – Steven A. Lowe Dec 31 '08 at 16:02
  • what you describe, in fact, is called Static Polymorphism in "C++ Templates The Complete Guide". – Özgür Mar 07 '09 at 16:49
  • @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 Mar 07 '09 at 20:10
4

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

duffymo
  • 305,152
  • 44
  • 369
  • 561
3

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
  • 62,228
  • 14
  • 110
  • 173
  • 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 Dec 31 '08 at 13:54
  • I agree with Mitchel, I often hear people lump encapsulation and abstraction in the same boat. – kemiller2002 Dec 31 '08 at 13:58
  • @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 Dec 31 '08 at 15:26
  • 1
    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. – SmacL Dec 31 '08 at 15:50
3

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
  • 958
  • 9
  • 31
1

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
  • 13,948
  • 9
  • 50
  • 70
1

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.

G S
  • 35,511
  • 22
  • 84
  • 118
1

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
  • 24,645
  • 16
  • 59
  • 83
  • It was the main feature of smalltalk, and main feature of the kind of OOP smalltalk provided. – Cheery Dec 31 '08 at 15:29
1

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
  • 34,834
  • 8
  • 106
  • 155
1

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.

vitoboski
  • 203
  • 2
  • 3
0

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

Perry Neal
  • 765
  • 4
  • 7
0

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.

0

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.

Rune FS
  • 21,497
  • 7
  • 62
  • 96