Reputation: 434
Ok conceptual difficulty here - Reading and learning Java from a book
It mentions the following (over the course of explaining several different subjects) -
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)
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
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!
Upvotes: 4
Views: 2133
Reputation: 718866
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.
Upvotes: 2
Reputation: 20163
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.
Upvotes: 1
Reputation: 11041
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 Object
s.
Upvotes: 2