Java and OOP

Just a few weeks ago Java 9 was released and there are a features that made me think: What is the future for this language? What directions has it taken? The very first thing that I learned (even heard) about Java as a programming language was that it was object-oriented. Just look at what Wikipedia (not the best source I know) has to say about it, this the first sentence:

Java is a general-purpose computer programming language that is concurrentclass-basedobject-oriented

As much as I agree on concurrent and class-based, I’m not so sure about object-oriented anymore. Actually, Java was never truly OO. OOP would mean we need objects to access methods and variables inside of a class. However this is not always true. Let’s look at the static keyword in java. Static members are class level and can be accessed directly without any instance. When everything in a class is static there can only ever be one instance of a class, i.e. all the variables etc. only exist once, resulting in the procedural programming style we were using before. But I guess it was a necessary exception, it was always there so it didn’t stop me from sleeping at night.

What caught my attention is that Oracle are moving more and more away from OOP principles. Take a look at what happened with the collections in Java 9. Now, we have static factory methods on the collection interfaces that will create concise, unmodifiable collection instances. Instead of:


Set<String> animalSet = new HashSet<>();

animalSet.add("monkey");

animalSet.add("penguin");

animalSet.add("rhino");

animalSet = Collections.unmodifiableSet(animalSet);

In Java 9 we have:

Set<String> animalSet = Set.of("monkey", "penguin", "rhino");

It’s short, concise, less ceremonial, readable. Is it object-oriented? – No.

Let’s go one more step back – Java 8 and the Stream API. It’s awesome, you get a lot of help to process your data right out of the box in a “declarative” way – filter(), limit(), sorted(), forEach(), etc. What is the problem? Do you know how big is the Stream interface? – There are 43 methods inside it. RIP Single Responsibility Principle and a few others as well.

Speaking of interfaces and Java 8… How about the introduction of default and static methods in interfaces? (And I thought explaining the difference between abstract class and an interface was difficult before…) One of the major reason for introducing default methods in interfaces is to enhance the Collections API in Java 8 to support lambda expressions. This to some extend I understand. On the other hand though, Java interface static method is similar to default method except that we can’t override them in the implementation classes. This feature is supposed to help us in avoiding undesired results in case of poor implementation in implementation classes. WTF??? The very nature of interfaces is to force the class to implement it’s methods. I simply cannot go around what was the intention in allowing such behavior.  Fortunately, the only place I’ve seen these interface methods being used is in the Java 8 tutorials. Let’s keep them there, please.

At the end of the day, no, Java is not dying. Stop craving for attention on LinkedIn. A few days ago, I saw statistics for 2017 saying Java is still most used programming language. The pressure sure is big though. Scala and Kotlin are the new hot thing right now. Oracle are certainly making some interesting design decision to respond to the competition. Is Java going to fight them? Or Oracle may even take over them considering they are already working on top of JVM?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s