Solved

upcasting down casting object remains same but refernece changes

Posted on 2016-08-16
8
60 Views
Last Modified: 2016-08-16
Hi,

When we do upcasting down casting and object instance why actual object still remains same but referenece changes? please advise
0
Comment
Question by:gudii9
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41758132
Please link to where you read that.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758153
public class Casting {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Derived drv = new Derived();

		Base base = (Base) drv;

		System.out.println("Class : " + base.getClass()); 

		
	}

}

Open in new window


public class Derived extends Base{

}

Open in new window


public class Base{

}

Open in new window


output is
Class : class Derived
i expected Base??
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41758158
i expected Base?
Why?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758170
package com.upcast;
class Base {
    public Base() {}
    public void foo() {
        System.out.println("I'm the base!");
    }
}

Open in new window

package com.upcast;

class Child extends Base {
    public Child() {}
    public void foo() {
        System.out.println("I'm the child!");
    }
}

Open in new window


package com.upcast;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Child x = new Child();
		Base y = (Base) x;
		y.foo();
	}

}

Open in new window

output
I'm the child!
i expected I'm the base! (as reference is Base type)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Author Comment

by:gudii9
ID: 41758172
i expected Base?
Why?

since we upcaste to Base from Derived?
0
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 41758193
Casting does not change the type of an object. It only changes the type of the reference to it. No matter how much casting you do to an object, calling getClass() on it will return the same class.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41758228
is it same with down casting? any good  example on down casting? do we do down casting also often as upcasting?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41758240
It's the same with any casting.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now