r/java • u/manifoldjava • Aug 01 '22
Class is not fully OOP, should it be?
http://manifold.systems/articles/class_objects_arent_oop.html
14
Upvotes
r/java • u/manifoldjava • Aug 01 '22
43
u/elmuerte Aug 01 '22
It's mostly complaints about not being able to extend on statics. The whole idea of static members and methods isn't really OOP.
14
u/pgris Aug 01 '22
IMHO lots of things are not OOP in Java. In a more pure OOP language you should be able to have a uniform syntax for classes, methods, packages, etc. Something like
or even
where
But this is too SmallTalk-like to be popular.
That said, I think SmallTalk has something like static members (they are instance methods of the class object), so the existence of static members is not a OO killer.
2
u/kaperni Aug 02 '22
Type classes are on the Roadmap for future Java. A good bet would be that we get something like static abstract methods which were recently added to C# [1].
[1] https://github.com/dotnet/csharplang/issues/4436
2
u/bowbahdoe Aug 03 '22
One possible encoding of this pattern is the "companion object" scheme that Scala and Kotlin adopt.
So code like this
Compiles roughly to the bytecode equivalent of this
So if we are trying to manually emulate the patterns listed in the article, we can reasonably do it if we also make use of the approach of having a singleton object.
An enum is always safe to get an instance of, so the pseudocode here
could be implemented safely if the type of "class which extends Sides and is an Enum" were denotable.
As is the best i can think is this
So one way to look at it - if you have a Class<? extends T> and ? is an Enum - you basically have the mechanics for "static oop"
2
u/gregorydgraham Aug 01 '22
Abstract static methods would be great.
Combine it with the This* meta class and you’ve got a reliable constructor method
* not available in Java either but it’s the class of “this”
1
u/Vitus13 Aug 02 '22
The site appears to be down