2

My doubt is the reference variable 'r' which was referencing rose object is now referencing flower object.

What happend to rose object now? Will it be destroyed?

I have the following code:

class Flower
{
public void smell()     // I
{
   System.out.println("All flowers give smell, if you can smell");
}
}   
public class Rose extends Flower
{
public void smell()     // II
{
   System.out.println("Rose gives rosy smell");
}
public static void main(String args[])
{
   Flower f = new Flower();
   Rose r = new Rose();

   f = r;             // subclass to super class, it is valid
   f.smell();           // II

 }
}
1
  • Your flower object will not have any references to it, so it will be cleaned up whenever the garbage collector runs (you don't have to worry about freeing the memory). Commented May 21, 2013 at 11:04

5 Answers 5

9

enter image description here

It is Flower object which is eligible for garbage collection. Rose object is still referred by both reference variable which is f and r.

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

3 Comments

what type of casting is this? up or down? can u explain this pls
Thanx Amit,so now, what wil be the type of Rose object,is it of Rose type or Flower type since it is being referenced by both the variables?
Now this is flower reference type for the object Rose. You can have Object class reference type as well. Reference type is always at the left hand side of the equal symbol and object type is always at the right side of the equal symbol.
4

Here you have assigned the rose to the f variable. This means that the instance of flower is now ready to be destroyed (garbaged collected).

f contains the rose, so f.smell() will result in Rose gives rosy smell.

1 Comment

Thanx,but i dint get one thing,was that upcasting or sowncastin? can u pls explain?
2

In your code, the instance of f = new Flower() is not used and will eventually be removed (means you cannot address it anymore and the garbage collection will most likely remove it sometime in the future).

This would be the same for the following code:

Rose one = new Rose();
Rose two = new Rose();
one = two;

the one = new Rose() variable wont be used and will eventually be removed. So that has nothing to do with casting in the first place.

As you can address all Rose Objects as Flower Objects you can assign a Rose instance to a Flower Variable.

1 Comment

"will be removed" is slightly problematic: there's no rule that says it must be removed. It's eligible to be removed, and it will probably be removed eventually (if the JVM lives long enough and an appropriate GC is triggered by some mechanisms). Exactly this "fuzzyness" is why strong wording such as "will be removed" can lead to a wrong impression.
1

If its created as a new Rose it is a Rose forever, casting does not affect the underlying object. However certain methods don't care if its a rose, just that its some kind of flower, they take a flower as their argument and the rose is treated as any old flower (but can be cast back to being a rose).

Equally in your case you have flower variable f, a rose is a flower so a rose can be held within a flower variable, but it is still fundamentally a rose.

Similarly an object that is created as a new flower can never be a rose, casting to rose will not cause a compile time exception because a flower could be a rose but will raise a run time exception because in this case it isnt.

As other answers have correctly said your "new flower" is never used and will be garbage collected

Comments

1

When the following code is run:

 Flower f = new Flower();//Line1
 Rose r = new Rose();//Line2
 f = r;   //Line3

This is what happens:

Line1 creates a new Flower object on the heap memory and the reference f will be on the stack.

Line2 creates a new Rose object on the heap memory and the reference r will be on the stack.

Line3 Now assigning f=r makes f also a reference to the same Rose object created in line2 as a result the reference to the flower object that was created at line1 will be lost so the flower object will remain unreferenced in the heap memory until it is garbage collected by the JVM and destroyed eventually.

Comments

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.