3

My first programming language is C++. C++ is a multi-paradigm programming language. One of the paradigms C++ supports is Object Oriented Programming. I want to say I get the general idea and benefits of OOP as is exemplified in C++.

I have recently started creating a project in Android Studio. One of the languages Android Studio supports is Java. Java is a 'pure' object oriented programming language. Pure is in scare quotes due to java supporting primitive data types.

My question is why does Java go full OOP where as C++ doesn't feel the need to go to that extent. C++'s approach seems more 'reasonable' to me. However, I'm sure there are specific reasons why Java does it the way it does. Where everything is an object, everything has to be declared as public, protected, etc. Everything is used loosely.

  • 2
    Try SmallTalk or Self - Those are Object Orientated languages no scare quotes needed. Java is more Object Orientated than C++ because of garbage collection. The reason why Java requires you to specify scoping (public/private/...) is because Objects are instances of complex types with a complex interface that restrict who can use portions of that interface. C++ does it too. What are the benefits? Wrong question. Which language is more natural for expressing the solution to the problem you have? That depends on the solution. – Kain0_0 Nov 19 at 2:09
  • 1
    @Kain0_0 can you expand on this part "Java is more Object Orientated than C++ because of garbage collection". Why does being more object oriented entail a connection with garbage collection, if I'm understanding it correctly. – FernandoH-G Nov 19 at 5:07
  • 1
    @DocBrown In a professional setting, Haskell. In an academic setting, probably everything under the sun 'cause my current teacher is a sadist. – T. Sar - Reinstate Monica Nov 19 at 16:37
  • 1
    RAM machines have addressable memory 0..(2^N)-1, where N is the bit size of the pointer. All memory addresses exist for the duration of the program. No new addresses will ever exist, nor can they be made to not exist. Therefor we must come to some consensus about what is at particular addresses. In C/C++ like languages the language defers that consensus to the programmer. Do you want to change how you look at a section of memory, just change the pointer type. No worries. But this makes GC impossible, without the programmer agreeing to play nice. That's a huge loop hole. – Kain0_0 Nov 20 at 22:25
  • 1
    The way to make GC possible is for the language to assume all responsibility for book keeping, and to take away from the programmer the ability to randomly anoite pieces of memory as being of a particular type. The GC can handle most memory itself as it only needs to be deallocated. This is good enough for not objects like int, and actual objects like String. But not for objects like File, these need to do something first. The GC cannot know what that something is in advance so it calls a function called a finaliser. Java provides this via an interface and only objects can implement that – Kain0_0 Nov 20 at 22:46
5

Java is indeed more object-oriented than C++:

  • Java’s object model is independent of the underlying memory model.
  • Some C++ object-oriented features need understanding of the underlying memory model; for example, to dynamically create/delete objects you need pointers or smart pointers and thus make the difference between the address of the object and the object itself. Also, in C++ you can create code independent from any class.

But let’s be provocative: Java is not a pure object oriented language:

  • not everything is an object in java: basic types such as int are not handled as objects and do not inherit from Object. You'd need to use an Integer class wrapper to achieve this.
  • static methods can be defined in a class but are not bound to any particular object. This feature makes Java more a class-oriented language than an object oriented one.

The increased object orientation has a couple of advantages:

  • increased portability (i.e. you just rely on an abstract object model).
  • consistency of the language constructs (everything is an object).

I don’t know if there are many pure OO languages out there. Smalltalk would be a good candidate. However it requires a paradigm shift, that makes even some simple code difficult to read.

In the end, OO is like oxygen (O₂): in reasonable dose it makes fresh air and enables complex life; in pure concentration it deprives air from other useful things. This certainly contributes to explain the current dominance of multi-paradigm languages.

  • Hey, C++ is just as "independent" of the underlying memory model as the Java language. Java just seems like it's independent because it runs in the JVMs memory model. Well C++ can do that too. ;) – candied_orange Nov 19 at 9:03
  • 2
    "But let’s be provocative: java is not a pure object oriented language" - I found the original claim that java is such a language" way more provocative. – Doc Brown Nov 19 at 9:16
  • @candied_orange As a C++ fanatic I’d be (almost) with you. But If you’re in OO, you’ll want sooner or later to use polymorphism. In C++ it requires to understand what a pointer is, and the difference between object and a pointer to the object. You then need to make the difference btw local and free store objects. It’s related to the memory model. I agree, it’s not much to know, but you can’t escape it. In java, with the reference semantic and uniform object creation and use, you don’t need to know these details at all. When teaching the language, this makes a lot of difference. – Christophe Nov 19 at 9:55
  • @candied_orange this being said, we can agree that in both languages we can write nice portable OO programs ;-) – Christophe Nov 19 at 9:57
  • @Christophe As in Java, where you also need to know the difference between a pointer (called reference) and an object. And only having one option is very different from it not making a difference. – Deduplicator Nov 20 at 5:42
6

The advantage of a 'pure' language of any paradigm is that you (ideally) don't have to keep reminding yourself to follow the paradigm since the language won't let you get anything done if you don't.

For example, Java is a 'pure' structural language. Which is just a fancy way of saying it has no goto. Instead you have while, do while, for, break, and continue. None of which you need if you have goto. It's the things you can't do in a language that make it 'pure'.

So what is that for object oriented languages? Primitives. If you want your language to be purely object oriented everything must be an object. No primitives.

If you find that unsatisfying I don't blame you. OOP's biggest feature is polymorphic message passing but the lack of primitives is what languages need to be considered 'pure' OO.

Sadly this means that even 'pure' OOP languages aren't forcing you to use the best features of OO. They're only forcing you to use the OOP features that can be forced.

  • None of which you need if you have goto -- You don't need goto either. You just need recursion and branching. – Robert Harvey Nov 19 at 6:27
  • @RobertHarvey Well no, recursion is only a perfect replacement for goto if you have infinite memory. My bus isn't that wide and my wallet isn't that deep. – candied_orange Nov 19 at 6:32
  • 1
    @J.AntonioPerez that would work. But now you've simply created another 'pure' language. Instead of structured programming it's tail call programming. – candied_orange Nov 19 at 7:41
  • 2
    @J.AntonioPerez If it was structured programming wouldn't exist. All you're offering is a different paradigm that would also keep goto's spaghetti code at bay. – candied_orange Nov 19 at 7:50
  • 2
    @DocBrown I can't disagree with your criticism. But I feel the flaw lies with the very concept of 'pure OO language'. Point me to one single 'pure' OO language that will keep a determined programmer from coding procedurally. I'll adopt it today. – candied_orange Nov 19 at 9:12
2

A key consideration in language design is complexity. How many features does the language have? How hard is it to learn? By reducing the number of features, you usually make the language easier to learn.

By following a single paradigm, you can can remove features.

Then there's program design. As a programmer, you have to think about how to structure your program. If your programming language gives you lots of flexibility in the structure, you may arrive at a better design in the end, but you also have to think through more possibilities. Working in a more constrained language often feels more productive (and might even be), because even if your design isn't as optimal as one you could have achieved in a more flexible language, you arrived there faster.

By following a single paradigm, you streamline the application design process.

As a consequence, then, if there are fewer possibilities for application design, if you encounter an unfamiliar codebase, it should be easier to understand its design, as there are only so many possibilities. This makes it easier to introduce new programmers to the development team.

By following a single paradigm, you narrow the design space, thus making onboarding of new team members easier.

  • This looks like a good start for an answer. Now explain how this maps to the design of Java and give some examples where these ideas were applied in the design of the language - then it could become a good answer. – Doc Brown Nov 19 at 9:22
0

In the case of Java and C++, C++ was based on C, so all the non-OO stuff was there for free. Removing it would have cost time and money, and would have made it much harder to move software from C to C++.

In the case of Java, they would have had to add extra work in the language, compiler, virtual machine, to make non-OO code work. JNI is kind of non-OO and that was only added later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.