# Object oriented language restrictions

**URL:** <https://boards.straightdope.com/t/object-oriented-language-restrictions/294971>\
**Category:** Factual Questions\
**Created:** [March 17, 2005, 12:39pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971 "2005-03-17T12:39:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [March 17, 2005, 12:39pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/1 "2005-03-17T12:39:29Z")

</div>

I remember years ago, when C++ first became popular, the compilers had restrictions so that certain object oriented features weren’t supported. Are there still common restrictions imposed on object oriented languages? If there are, then what object oriented language / compiler has the most complete coverage of object oriented concepts?

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [March 17, 2005, 1:52pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/2 "2005-03-17T13:52:01Z")

</div>

The restrictions in C++ are mostly to do with the incredible difficulty of implementing a compiler which implements all the features required by the C++ standard. I’m not aware of any compiler that is 100% compliant with the standard, although I heard Comeau C++ is getting pretty close.

---

<div class="post-metadata">

**Author:** ![Balthisar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/balthisar/32/2825_2.png) [@Balthisar](https://boards.straightdope.com/u/Balthisar)\
**Post date:** [March 17, 2005, 2:04pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/3 "2005-03-17T14:04:21Z")

</div>

What, specifically, do you mean? There are lots of philosophies about object oriented languages. E.g., you can’t compare even C++ with Objective-C. Object Pascal goes another way. Visual Basic is in some vague way object oriented.

Do you have an example of what you’re asking?

---

<div class="post-metadata">

**Author:** ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)\
**Post date:** [March 17, 2005, 3:14pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/4 "2005-03-17T15:14:13Z")

</div>

Seeing as nobody can really agree on what “object-oriented” means, it’s a rather difficult question to answer. Certainly there are lists of features that various environments tend to pick and choose from. Perl has multiple inherritence, polymorphism and dynamic dispatch, but no protected or private data members.[sup]1[/sup]. Java has only single inherritence and does support protected data. And ten thousand other comparisons of this type can be made. This isn’t due to any particular problem with compilers, just language design.

[sup]1. There are neat tricks you can do with closures and lexical scoping to simulate private data in Perl classes, though. [/sup]

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 17, 2005, 4:06pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/5 "2005-03-17T16:06:35Z")

</div>

The most common definition I’ve seen is that an object-oriented language is one that supports polymorphic variables. However, most people would balk at the suggestion that VB isn’t object oriented, so maybe it’s a fuzzy property.

In addition to the question of what features a language has, there’s also the question of how to handle issues that every language will have to deal with. Say you have classes C -\> B -\> A, and someone invokes a method on an object of type C that isn’t found in C’s set of methods. Should the compiler start looking in B and move up to A, or should it start looking in A and move down to B? C++ does the former, and (IIRC) Java does the latter.

That doesn’t even touch on object-oriented languages that don’t have classes…

---

<div class="post-metadata">

**Author:** ![mattmorgan64](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@mattmorgan64](https://boards.straightdope.com/u/mattmorgan64)\
**Post date:** [March 17, 2005, 4:06pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/6 "2005-03-17T16:06:40Z")

</div>

All I know is that I started out in '82 by first learning ‘C’/Unix, and then Pascal, then on to COBOL, BASIC, and PC’s. (Sort of retarted, yes, but you have to learn what is in use when the Military ships you somewhere).

Anyway, to this day I’m still not an OO programmer, even though I’ve been trying since about '87 when Borland released Object Pascal.

I understand the high level concepts about OO. I just seem unable to make the great conceptual leap and it pisses me off to no end.

Sorry for the highjack.

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 17, 2005, 4:13pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/7 "2005-03-17T16:13:33Z")

</div>

> [@ultrafilter](#):
>
> C++ does the former, and (IIRC) Java does the latter.

C++ will do the latter if the method is declared virtual and the former if it isn’t.

If the answer was any simpler then that, it just wouldn’t be C++. 🙂

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 17, 2005, 4:40pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/8 "2005-03-17T16:40:43Z")

</div>

> [@Metacom](#):
>
> C++ will do the latter if the method is declared virtual and the former if it isn’t.

Actually, my brain misfired and I didn’t parse the issue correctly.

I should have said: C++ will do the latter if the method is a pure virtual function. If the method was a regular virtual function, then it would exist in C’s set of methods; if it wasn’t defined at all in C then there would be a compile-time error.

Actually, this is identical to Java’s semantics–just substitute “abstract method” for “pure virtual function.”

**ultralifter** , were you thinking of the whole “methods aren’t dynamically dispatched by default in C++ but they are in Java” thing?

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 17, 2005, 5:00pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/9 "2005-03-17T17:00:00Z")

</div>

There’s existing in C’s methods, and there’s being declared there. I meant the latter.

But are you sure about that? When I run the code below in MS [VC.net](http://VC.net) '03, I get the output of B::foo. I’d hate to think something like that was compiler specific.

```auto

class Z
{
	public:
		virtual void foo() = 0;
};

class A : public Z
{
	public:
		virtual void foo() { printf( "Me = A.
" ); }
};

class B : public A
{
	public:
		virtual void foo() { printf( "Me = B.
" ); }
};

class C : public B
{
};

int main()
{
	C c;
	c.foo();

	return 0;
}

```

I’m not incredibly familiar with Java, so the default dynamic dispatch is new to me. Does that mean that no method ever gets deadstripped by the linker?

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 17, 2005, 5:30pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/10 "2005-03-17T17:30:31Z")

</div>

> [@ultrafilter](#):
>
> But are you sure about that?

Your example makes it MUCH clearer–when you first wrote “C → B → A” I parsed that as “C, it’s subclass B, and B’s subclass A”. I’m too used to diagrams of trees with the root node at the top. 🙂

Here’s a Java equivalent to your C++ example. When I run it, it B’s foo is what gets called:

```auto

abstract class Z {
  abstract public void foo();
}

class A extends Z {
  public void foo() {
    System.out.println("Me = A.");
  }
}

class B extends A {
  public void foo() {
    System.out.println("Me = B.");
  }
}

class C extends B {

}

public class Test {
  public static void main(String[] args) {
    C c = new C();
    c.foo();
  }
}

```

> [@ultralifter](#):
>
> I’m not incredibly familiar with Java, so the default dynamic dispatch is new to me. Does that mean that no method ever gets deadstripped by the linker?

Linker? What’s that? 😉

A .class file (the bytecode equivalent of an object file) includes all the methods of a class. You run a Java program by starting up a VM and telling it the name of the class whose “main” method you wish to invoke. It then loads the definitions of the classes that the code in “main” requires on an as-needed basis from .class files (or some package containing them).

And, come to think of it, every implementation of Java (even ones that compile straight-to-native code), would have to keep all (public) methods accesible in perpetuity in all programs: Java’s “reflection” facilities allow you to instantiate objects and call methods that aren’t actually mentioned in the source code. E.g.:

```auto

...

      void callArbitraryMethod(String nameOfClass, String nameOfMethod) throws Exception { 
        Class theClass = Class.forName(nameOfClass);
        Object theObject = theClass.getConstructor(null).newInstance(null);
        Method theMethod = theClass.getMethod(nameOfMethod);
        theMethod.invoke(theMethod, theObject);
    }

...

```

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 17, 2005, 5:40pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/11 "2005-03-17T17:40:04Z")

</div>

This is getting a bit far off topic…

The point is, aside from the issue of whether a language has a particular feature, there are also often choices about how to make it behave, so that muddies the question a little bit.

---

<div class="post-metadata">

**Author:** ![ccwaterback](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@ccwaterback](https://boards.straightdope.com/u/ccwaterback)\
**Post date:** [March 18, 2005, 3:33am UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/12 "2005-03-18T03:33:00Z")

</div>

So there are definitely still restrictions out there, or at least implementation nuances. I don’t know, I find a lot of C++ and Java to be very clumsy and esoteric, but I suppose I will always feel that way being an assembly language programmer at heart.

🙂

---

<div class="post-metadata">

**Author:** ![Small\_Clanger](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Small\_Clanger](https://boards.straightdope.com/u/Small_Clanger)\
**Post date:** [March 18, 2005, 10:58am UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/13 "2005-03-18T10:58:11Z")

</div>

> [@mattmorgan64](#):
>
> I understand the high level concepts about OO. I just seem unable to make the great conceptual leap and it pisses me off to no end.

It took me several attempts, it finally clicked after a week or so of immersing myself in C# I found OO to be one of those things that only makes sense once you already understand it.

In C you are used to doing things directly say:

bash\_nail\_into\_plank(nail\_ptr, plank\_ptr);

The OO version will look more like:

h = new Hammer;  
n = new Nail;  
p = new Plank;

h.bashIn( n, p );

Which initially looks like a lot of palaver. What it really means is that -with luck- someone has already defined the Hammer, Nail and Plank classes and you just pull them off the shelf and use them rather than having to detail what to do in your bash\_nail\_into\_plank() function.

---

<div class="post-metadata">

**Author:** ![Small\_Clanger](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Small\_Clanger](https://boards.straightdope.com/u/Small_Clanger)\
**Post date:** [March 18, 2005, 11:31am UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/14 "2005-03-18T11:31:40Z")

</div>

Fiddlesticks. Should be:

Hammer h = new Hammer\*\*()**;  
Nail n = new Nail**()**;  
Plank p = new Plank**()\*\*;

Guess I’m rusty, they keep me busy with C and PL/SQL, haven’t done OO in months ☹

---

<div class="post-metadata">

**Author:** ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)\
**Post date:** [March 18, 2005, 4:23pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/15 "2005-03-18T16:23:08Z")

</div>

> [@Small Clanger](#):
>
> Fiddlesticks. Should be:
> 
> Hammer h = new Hammer\*\*()**;  
> Nail n = new Nail**()**;  
> Plank p = new Plank**()\*\*;
> 
> Guess I’m rusty, they keep me busy with C and PL/SQL, haven’t done OO in months ☹

In C++, both Hammer h = new Hammer; and Hammer h = new Hammer(); would compile. Hammer h(); is not legal because the compiler thinks it’s a function definition.

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 18, 2005, 4:30pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/16 "2005-03-18T16:30:33Z")

</div>

> [@ccwaterback](#):
>
> So there are definitely still restrictions out there, or at least implementation nuances. I don’t know, I find a lot of C++ and Java to be very clumsy and esoteric, but I suppose I will always feel that way being an assembly language programmer at heart.

If you’re having trouble “Getting” OOP, I recommend complete immersion.

Download [Squeak](http://www.squeak.org/), get a [good book on squeak](http://www.amazon.com/exec/obidos/tg/detail/-/0201731142/qid=1111164542/sr=8-1/ref=sr_8_xs_ap_i1_xgl14/102-6922610-6127350?v=glance&s=books&n=507846), and become a Smalltalk hacker. You’ll instantly be way cooler then people who use Java or C++ for OOP. 😉

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 18, 2005, 4:40pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/17 "2005-03-18T16:40:33Z")

</div>

> [@ultrafilter](#):
>
> In C++, both Hammer h = new Hammer; and Hammer h = new Hammer(); would compile. Hammer h(); is not legal because the compiler thinks it’s a function definition.

Nitpick:

I don’t think either of those would compile, 'cause h isn’t declared to be a pointer?

Also, wouldn’t Hammer h() compile just fine? It would stack-allocate an instance of h and invoke its zero-arg constructor.

---

<div class="post-metadata">

**Author:** ![Small\_Clanger](https://avatars.discourse-cdn.com/v4/letter/s/9fc348/32.png) [@Small\_Clanger](https://boards.straightdope.com/u/Small_Clanger)\
**Post date:** [March 18, 2005, 5:09pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/18 "2005-03-18T17:09:12Z")

</div>

> [@Metacom](#):
>
> . . . wouldn’t Hammer h() compile just fine? It would stack-allocate an instance of h and invoke its zero-arg constructor.

**mattmorgan** you getting all this 🙂

Maybe some of these variations will compile. This is (part of) the problem of trying to learn OO though C++, it is very hard to see the wood for the ~::\<\<ing trees.

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 18, 2005, 5:18pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/19 "2005-03-18T17:18:25Z")

</div>

> [@Small Clanger](#):
>
> This is (part of) the problem of trying to learn OO though C++, it is very hard to see the wood for the ~::\<\<ing trees.

Seconded. I would put a beginning programmer or someone new to OOP on C++. Unless I really, really hated them. 🙂

If you’re new to OOP (or even new to programming), I’d seriously look at Squeak.

If its too esoteric for you, I’d say even Python would be a better language to cut your teeth on.

---

<div class="post-metadata">

**Author:** ![Metacom](https://avatars.discourse-cdn.com/v4/letter/m/ecae2f/32.png) [@Metacom](https://boards.straightdope.com/u/Metacom)\
**Post date:** [March 18, 2005, 5:19pm UTC](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971/20 "2005-03-18T17:19:29Z")

</div>

Doh! I would _NEVER_ put a begginner on C++.

I left out an important word, there.

[Next page](https://boards.straightdope.com/t/object-oriented-language-restrictions/294971.md?page=2)
