4

Ok conceptual difficulty here - Reading and learning Java from a book

It mentions the following (over the course of explaining several different subjects) -

  1. Java does not support multiple inheritance in the class hierarchy. So if class C is a child class of class A, then it cant also be a child class of class B (I know interfaces have a solution to offer here)

  2. java.lang.Object is the primordial class that all classes (system and user) 'extend'. So every class is a direct or indirect child of the class java.lang.Object

  3. classes in Java are not just compiler artefacts - but at run time, are represented by the instances of the class java.lang.Class

Doesn't #3 mean classes defined in the language themselves are of type java.lang.Class - while #2 means they are also of type java.lang.Object - contradicting #1 ? like which class are java classes children to?

And if #1 still holds, I am guessing it would mean that #3 is just saying the "runtime representation" of classes are instances of the class java.lang.Class - which is a child of java.lang.Object??, but the classes themselves are children to java.lang.Object??

Your guidance is appreciated!! Thanks in advance!

3
  • You're confusing class and instance of a class. A class is an instance of the class Class, and Class, like all classes, is a subclass of Object. An instance of, say, String is not an instance of Class, nor is it a subclass of Class. It's just that the String class itself is an instance of Class (and not an instance of String). Commented Jul 13, 2014 at 2:25
  • 2
    (To really blow your mind, consider that the class Class itself is an instance of Class, and, furthermore, the class Object is also an instance of Class.) Commented Jul 13, 2014 at 2:27
  • (Oh, Class<Class> clazz = Class.class works. Now I'm really confused.) Commented Jul 13, 2014 at 2:28

3 Answers 3

2

Doesn't #3 mean classes defined in the language themselves are of type java.lang.Class

No. (Class) testclass is syntax error - No classes are subclasses of java.lang.Class - It's final.

Class extends Object and represents a class.

Let's say I have TestClass which extends Object by default, now TestClass.class or (TestClass).getClass() exists - they return Class<TestClass>, which extends Object and is an instance of Class.

But if you look into the Java documentation for Class, you'll see they are more than Objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I get it now - it just seems a bit odd to think Class is a subtype of Object - but once I wrap my head around that it does make sense
2

3) classes in Java are not just compiler artifacts - but at run time, are represented by the instances of the class java.lang.Class

That is correct.

Doesn't #3 mean classes defined in the language themselves are of type java.lang.Class

That is correct.

while #2 means they are also of type java.lang.Object

That is correct.

... contradicting #1 ?

That is incorrect. There is no contradiction.

The "extends" relationship is defined ONLY between classes ... and between the Class objects that represent the classes at runtime. If you look at the java.lang.Class API you will see methods such as Class getSuperclass() and isAssignableFrom(Class) ... though isAssignableFrom is testing the "subtype" relationship. Note that these methods take another Class as an argument, or return another Class.

There is no "inherits" or "extends" relationship between classes and instances. And there is no such relationship between the runtime objects either. The fact that java.lang.Class extends java.lang.Object is not relevant here.


And if #1 still holds, I am guessing it would mean that #3 is just saying the "runtime representation" of classes are instances of the class java.lang.Class - which is a child of java.lang.Object?

That is correct.

.... but the classes themselves are children to java.lang.Object?

That is also correct.

However the thing to note is that these two properties don't directly follow from each other. It is (sort of) coincidental that there are similar-looking relationships between classes and the runtime classes that represent classes.

Comments

1

3) classes in Java are not just compiler artifacts - but at run time, are represented by the instances of the class java.lang.Class

This is not correct. Java objects are objects. They also have an attribute that represents their class type.

public class Test  {
   public static final void main(String[] ignored)  {
      String s = "x";
      Class cls = s.getClass();
      System.out.println("s=" + s.toString());
      System.out.println("cls=" + cls.toString());  
   }
}

Output:

s=x
cls=class java.lang.String

It's confusing that we say "Java objects are classes". These is true in the general sense, but with this specific technical detail, it's inaccurate.

Loosely speaking, Java objects are stored in *.class files, which is something else that makes this distinction confusing.

10 Comments

Would it be less confusing if they were stored in *.object files?
@HotLicks I don't understand. Is that a joke?
@HotLicks Well I'm not getting it. Are you suggesting something could be improved in my answer?
@HotLicks It is very low "class" to make me the "object" of your joke. ;)
What can I say? I'm an old assembler guy at heart -- no class at all.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.