Java Java Data Structures Getting There Type Casting

Sandro Müller
Sandro Müller
1,089 Points

Did I understand it correctly? (Object class, superclass, downcast, upcast)

My question 1: Is Object a superclass of all classes? Apparently there is a invisible Object class. Let's say I create a class Sandro{..}, then Sandro is from type Sandro and from type Object, because every class is a descendant of that class and so all of its objects?

My question 2: What is a superclass? Apparently, there can be a class that is in it's hierarchy higher than other classes. This comes with the advantages that a subclass can inherit all methods from a superclass and even overwrite and inherited method. I think I got this one.

My question 3: What is happening here? Object obj = treet: Do we create a new Object called obj and give it the methods of treet? Do I call the object like this: obj.methodName()?

My question 4: What is happening here: Treet treet = (Treet) obj? I understand that we need a casting, since treet is from hierarchy lower that Object obj. But does the object treet becomes a superclass now?

2 Answers

Boban Talevski
Boban Talevski
24,780 Points

-1. Yes to everything you assumed.

-2. Yes you did :)

-3. We are not creating a new object since we are not using the new keyword. We are just creating a new object reference obj, which is of type Object, and we make it reference the same object as the one referenced by treet. And we can safely do that since whatever type of object treet is, it extends Object according to 1. and 2. above.

We are not giving it any new methods this way since obj is still of type Object. If we downcast it to Treet, it'll get access to the methods defined in Treet class. And I'm not sure what you mean by "Do I call the object like this: obj.methodName()?" You are calling a method on the object obj this way, but it can't be a method of the Treet class.

-4. This is the cast I mentioned in 3. where we would downcast the object obj to Treet type, so that obj will now be able to call methods in the Treet class. Note that the cast isn't permanent, it's just for this line of code, if we use obj in the next line of code, it's still of type Object and we can't call Treet methods, while in the line where we cast it to Treet we can.

And no, Treet doesn't become a superclass of Object with this cast. Take in account we are manipulating objects here, we can't really manipulate classes in code, those are already defined, and are blueprints for our created objects. Manipulating objects doesn't affect class definitions at all, nor the inheritance hierarchy. After all, it's just one object of the said class, we could have hundreds of objects at any given time in an application, and all of them can be cast to different types independently without affecting the class definitions or the inheritance hierarchy.

Sandro Müller
Sandro Müller
1,089 Points

Thanks for your detailed explanation. I guess I grasp then concept a bit better. So when I create a new reference Object like that 1) Object obj = tree and 2) Tree tree = (Tree)object

1) obj instanceof(Tree); would be true and obj instanceof(Object); would be true? whereas

2) tree instanceof(Tree) would be true and tree instanceof(obj) would be false?

This is my 2nd week Java, ofc my questions will be weird. xd

Boban Talevski
Boban Talevski
24,780 Points

First of all, these assumptions aren't complete. I mean 1) Object obj = tree and 2) Tree tree = (Tree)object. No new objects are created here, so technically these lines of code are going to give errors and answers to your questions depend on what objects we actually create. In order to actually have an object, we have to create it using the new keyword. The new keyword is creating the actual object instance and that's why instanceof depends on what was used here to instantiate the object.

If we assume that tree was created as new Tree() and obj as new Object(), then tree will be instanceof both Tree and Object, while obj will only be instanceof Object and not Tree. Whatever new object reference we assign to these objects will be the same instanceof as the "original" object.

You could use that java repl thing Craig uses in the early courses (this one also probably) and do all these checks. But in any case, I think you got most of it right and shouldn't spend so much time on these details now. All will become clearer as you move on and you can always revisit the basics to clear things up, and it'll be much more understandable then :).