r/java • • Aug 01 '22

Class is not fully OOP, should it be?

http://manifold.systems/articles/class_objects_arent_oop.html
14 Upvotes

40 comments sorted by

View all comments

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.

6

u/manifoldjava Aug 01 '22

Yeah, I get that, you're referring to static members from the perspective of the user-defined class. But there's another level of use proposed here -- the "meta" level. Sorry if that wasn't clear enough in the post.

10

u/Norandran Aug 01 '22

Can you explain the meta level because frankly I don’t get it.

10

u/manifoldjava Aug 01 '22

I'll try.

The Class class is Java's meta-class. The post is basically saying, why not leverage the generic aspect of Class to OOP enable user-defined class methods (as opposed to instance methods)? The examples in the post get into the details of how that can be useful.

As I mentioned in the post I run into this occasionally, typically building meta-level stuff like code generators, pluggable things, etc.

I'm not saying this stuff _should_ be implemented. I'm saying _maybe_ it should be simply based on the number of incidents I personally bump into the subject.

_shrug_

4

u/westwoo Aug 02 '22 edited Aug 02 '22

Afaik statics extensibility has nothing to do with theory and instead is a practical artificial limit to avoid certain antipatterns

Java historically has been very conservative and handholding in the architectural department, trying hard not to give the tools to programmers that they could feasibly abuse

This has been largely changing though starting with Java 8 in particular

6

u/larsga Aug 02 '22 edited Aug 02 '22

Afaik statics extensibility has nothing to do with theory and instead is a practical artificial limit to avoid certain antipatterns

Extending statics makes no sense with Java as it is now.

When you extend class A and override method m in class B, which version of M gets invoked when you do obj.m() depends on whether obj is an instance of A or B.

But with statics there is no instance. The only way you can invoke a static method is by referring to the class explicitly. But then extending basically has no effect.

I didn't read OP's post, so they may have something more in mind than simply allowing static extensibility.

4

u/westwoo Aug 02 '22 edited Aug 02 '22

You don't have use the class name when you're referencing them from inside the class

So, for example, you could create AlienMath class that inherits from Math, overrides the Pi value and sin and cos methods, and all the other methods will automatically switch to using them. You could then write other classes with injectable Math.class so you could plug your AlienMath inside transparently. But of course it would arguably be a massively dirty and invasive and error prone and ideologically incorrect kind of "inheritance".

As for the technical arguments - eh, I'm not convinced by them. There are numerous ways to implement inheritance of statics if people actually wanted to do that. It doesn't make sense in the present implementation just because people didn't want to implement inheritable statics

2

u/larsga Aug 02 '22

So, for example, you could create AlienMath class that inherits from Math, overrides the Pi value and sin and cos methods, and all the other methods will automatically switch to using them.

Well, sort of. In AlienMath you'd switch, but not in Math. It would work, but it becomes a kind of automatic renaming more than anything else.

You could then write other classes with injectable Math.class so you could plug your AlienMath inside transparently.

No. Static methods are resolved at compile time, so you can't get runtime switching of static methods.

You'd need to add some way to have class variables that are recognized as such and treated specially when invoking static methods. I'm not sure I see the point, tbh. If you care that much about it being dynamic, why not just make a dynamic implementation.

1

u/westwoo Aug 02 '22

Sure, but that's how inheritance in general works - you override things without changing the original

As for the implementation - I'm sure there could be ways even without breaking the backwards compatibility, say through some additional proxies. Having a statically resolved proxy that resolves the implementation dynamically if needed isn't really a huge problem. But I doubt it will ever happen because the demand for it haven't massively changed, like it did for functional programming

1

u/manifoldjava Aug 02 '22 edited Aug 02 '22

Essentially, the receiver of the method call is an instance of Class.

public int words( Class<? extends Number> cls ) {
  return cls.bits() / 32; // <~~~ receiver is Class instance
}
  
public class Long extends Number {
  @Override
  public static int bits() {return 2 * 32;}
}
  
public abstract class Number {
  public static int bits() {return 32;}
}

The examples in the post clearly illustrate the concept.

1

u/duongdominhchau Aug 02 '22 edited Aug 02 '22

So, you want something like Ruby?

Edit: To clarify, in Ruby class is just another object, and Ruby allows you to define a method specifically for a single object. Therefore, instead of static method, in Ruby you have a method defined only for the class object. Because that is just instance method on a special object, you can still have your method overriding.

1

u/[deleted] Aug 04 '22

Ruby is definitely more flexible than Java here.

I'd wish Java would learn from the good parts of Ruby. The OOP of ruby makes a lot more sense to me than Java. But my biggest complaint right now is how verbose Java is.

1

u/thephotoman Aug 04 '22

There are good reasons that the Class<T> type is final. It turns out that allowing extension or modification of core language classes in object oriented languages can cause some very real security, readability, and maintainability concerns. I genuinely don't know enough about Ruby to tell you why it isn't a problem in that language (I mean, I know about how much metaprogramming it allows, but I don't know its security or execution models--I've not done anything in Ruby in over 15 years).

Then there's the reality of how monkeying about with core language classes can actually make your code significantly less readable (and therefore harder to maintain). Java has already sacrificed a lot of otherwise useful features to ensure that the language remains readable (e.g. we will never have operator overloading for that very reason). And honestly, I'd be much less irate if some of those features entered the language than I would be if Oracle suddenly decided to allow all classes to be open to extension and modification.

Honestly, if you're messing about in the whole sun.misc.Unsafe world, you're braver than I am.

1

u/manifoldjava Aug 04 '22

The post is not about extending Class<T>. It proposes to leverage the generic aspect of Class so that static methods may be invoked from instances of Class. For instance, an instance of Class<? extends CharSequence> could act as a receiver for virtual static method overrides on subclasses of CharSequence.

If you read the post, the examples clearly illustrate this concept.

1

u/youjiba Aug 15 '22

You clearly don’t cook it like I prefer

2

u/2bdb2 Aug 02 '22

Have a look at Scala. Specifically, Typeclasses.

Your words example would look something like this

// Define the typeclass
trait Sized[T] {  
  def bits:Int
}

// Provide an instance for Long and Int
given Sized[Long] with 
  def bits = 64

given Sized[Int] with 
  def bits = 32

// Boilerplate
def sized[T](using Sized[T]) = summon

// Define a function that uses the Sized typeclass
def words[T : Sized]:Int = sized[T].bits / 16



// Usage
println( words[Int] ) // prints '2'
println( words[Long] ) // prints '4'
println( words[String] ) // Compile error