7 ms·
People coming from Javaville or C#land definitely don't have that kind of abstraction (and yes, the usual rebuttal is that they're procedural, not OO).
by Fellshard 8y ago
People coming from Javaville or C#land definitely don't have that kind of abstraction (and yes, the usual rebuttal is that they're procedural, not OO).
- chrisseaton 8y ago> what do you mean a class is an object > People coming from Javaville or C#land definitely don't have that kind of abstraction What do you mean? A class is an object in both Java and C#.
- Fellshard 8y agoNot exactly. What you see as the Class type in Java, at least, is simply a representation of a class. It can tell you about a class, but it doesn't represent an actual thing you can manipulate, change, or use. I can't add a method to that class, or add a field, or manipulate its instances beyond basic method lookup and dispatch. In something like Ruby or Smalltalk, a Class is an object. It can be changed live, altered, adapted, cloned, or otherwise used just like another object; and the application will change its behavior accordingly, right there on the spot. I'd recommend reading up on prototype inheritance, as that is the model generally used in the latter languages.
- chrisseaton 8y agoYou’re telling me that class objects in Java are immutable while they’re mutable in Ruby. That’s orthogonal to whether they’re objects. > I'd recommend reading up on prototype inheritance Thanks but I’ve already literally got my PhD in metaprogramming in Ruby and object model implementation in Java. Ruby doesn’t use anything like prototype inheritance.
- kazagistar 8y agoIts more then that... It's not even inmutable, not really. There are some things, like privaCy of access, that can be mutated by manipulating Java class objects. That said, maybe the difference here is how often the janky metaprogramming of classes interferes with day to day peogramming; a java programmer might have never seen this stuff, moreso then a ruby one?
- Fellshard 8y agoYeah, the correction re: prototype inheritance is a good one. That said, there is a qualitative difference in what we call 'classes' in those two environments, which is part of why I think there's confusion. Class in Java is a fairly different concept than a Class in Smalltalk, Ruby, or JS.
- jonathankoren 8y agoJava has Class which is an Object.
- repsilat 8y agoHonest question: Can you write Class foo = String; in Java? If so I guess classes really are objects, if not I'd say "there are some objects that represent (or wrap) some classes somehow."
- akdas 8y agoClass foo = String.class; (Though `Class` is actually a generic type, so you'd want to include the type parameter in actual code.)
- repsilat 8y agoSure, I'd say that fits my second suggested wording -- `String.class` is an object that represents or wraps the `String` class.
- chrisseaton 8y agoHow do you tell the difference between an object that is the String class, and an object that represents the String class? If you can call .toString on it, and you can compare it to other classes, and you can call .newInstance, then how is it more of a representation than being the actual thing?
- lmm 8y agoIn java you have to call the special and awkward .newInstance rather than being able to use standard "new". Whereas in e.g. Python I can equally well do >>> str("foo") 'foo' >>> x = str >>> x("foo") 'foo' and the "builtin" str and "userspace" x are equally first-class.
- 8y ago
- jcranmer 8y agoClasses are objects in Java. You can even create new classes at runtime in Java or use java.lang.reflect.Proxy to make existing objects implement different interfaces. I once did this in a class project, including even manually creating the bytecode at runtime. Ironically, it's probably the most understandable and maintainable of the crazy metaclass-style reflection I've done (although that's probably mostly due to the fact that the scope was much more limited: I was only mirroring a set of interfaces into a different namespace to work around some stupid limitation in a different library).
- Fellshard 8y agoYou can, yes, but not nearly in the same way.