• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some doubts about casting

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I wanted to check situation where, the ambiguity arise in calling overloaded method. I was unable to think such situation(Which I saw as a question in one of the books that I have). I am now stuck in casting.

I have this code.



According to my understanding in the above program, there are 2 objects 1>child(referred by parent) 2>parent(referred by parent)

My query:

1> Is (Parent)pc is a new(temporary) object?
2> Is it created on the heap (without a new operator)?
3> Are there 3 objects in the above code?
4> Is it eligible for garbage collection soon after the println statement?

Regards,
Prathima
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:1> Is (Parent)pc is a new(temporary) object?

NO! It's not a new object. You still have 2 objects (a Parent instance and a Child instance). It's very important to know/understand, that's why I really stressed the "no" answer

Prathima gaitonde wrote:2> Is it created on the heap (without a new operator)?

It's not an object, so it's not created on the heap.

Prathima gaitonde wrote:3> Are there 3 objects in the above code?

No, just 2!

Prathima gaitonde wrote:4> Is it eligible for garbage collection soon after the println statement?

It's not an object, so not eligible for GC.


Hope it helps!
Best wishes for 2015!
Kind regards,
Roel
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Roel De Nijs wrote:
NO! It's not a new object.



What is it then,
1> A reference variable?
2> What is it referring?
3> As I can, call Parent methods using it. I take its not referring to null right?
4> Can you please explain me what is happening(internally) when we cast like this, (Parent)pc.


Thanks,
Prathima

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:1> A reference variable?
2> What is it referring?
3> As I can, call Parent methods using it. I take its not referring to null right?
4> Can you please explain me what is happening(internally) when we cast like this, (Parent)pc.

If you make a small change to your application, you can probably answer these questions for yourself.

Let's see if you can if I provide the modified code for you:


Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:According to my understanding in the above program

I forgot to mention in my first post: always make sure when you post code, the code compiles (unless you of course have some doubt/question about a compiler error ). Both class declarations of Parent and Family don't compile.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! I got my answer, I found the mistakes in my original code. Apologies for that, will promise to take care of the code, while posting it.

With Regards,
Prathima
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:Yes! I got my answer

You know the cast is in this case not required, because the type of the reference variable is already Parent.


And if the reference type is a subclass (like Child), the cast is done implicitly. So you are not required to add the cast, but you may do it. This makes sense: if Child extends Parent, it can do everything Parent can do as well.


If the reference type is a superclass (like Parent), you need to add the cast explicitly. Otherwise the code won't compile.


But with casting you always have to be very careful! What is the output of this code snippet?


Hope it helps!
Kind regards,
Roel

PS. This is my 1st post of 2015!
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:


But with casting you always have to be very careful! What is the output of this code snippet?




This code throws an exception, ClassCastException. I remember you had correct me, in one of my posts, I wrote "I know that when parent object is casted to child its an exception".

you wrote this in reply, "That's not (always) true! It all depends on the type of object the reference variable is referring to." Which helped me in many ways, that I came to know this!!,

From my original post (correct code from your post! ):



Thanks for the support, in my journey. Happy new year to all the javaranch users.

With kind regards,
Prathima.
 
Sibendu Dey
Greenhorn
Posts: 20
Java Oracle Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:According to my understanding in the above program, there are 2 objects 1>child(referred by parent) 2>parent(referred by parent)

My query:

1> Is (Parent)pc is a new(temporary) object?

No pc is not a new temporary object . When we write a statement such as (parent)pc , we are just upcasting the reference to become a parent reference variable

Prathima gaitonde wrote:2> Is it created on the heap (without a new operator)?

No they are not always created on the heap. If a reference variable is an instance or class variable , they are created on the heap. If they are local variables declared inside a block , they are created on the stack, but, the object to which they refer are created on the heap.

Prathima gaitonde wrote:3> Are there 3 objects in the above code?

No there are two objects . Just keep in mind objects can be created only using "NEW" statement.

Prathima gaitonde wrote:4> Is it eligible for garbage collection soon after the println statement?

Yes they are eligible for garbage collection since the block or method in which the reference variables are declared goes out of scope i.e.main() method.Hence,there is no way to refer to those objects created inside main().

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sibendu Dey wrote:
Prathima gaitonde wrote:3> Are there 3 objects in the above code?

No there are two objects . Just keep in mind objects can be created only using "NEW" statement.

2 little nitpicky comments:
1/ Java is case-sensitive, so it's new
2/ With strings and primitive wrapper classes, it's not 100% the case:

I created 2 new objects without using the new keyword.

Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:
Roel De Nijs wrote:
But with casting you always have to be very careful! What is the output of this code snippet?

This code throws an exception, ClassCastException.

Excellent!
 
Sibendu Dey
Greenhorn
Posts: 20
Java Oracle Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Sibendu Dey wrote:
Prathima gaitonde wrote:3> Are there 3 objects in the above code?

No there are two objects . Just keep in mind objects can be created only using "NEW" statement.

2 little nitpicky comments:
1/ Java is case-sensitive, so it's new
2/ With strings and primitive wrapper classes, it's not 100% the case:

I created 2 new objects without using the new keyword.

Kind regards,
Roel


Oops looks like I missed that point. Thanks for pointing that out !!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic