Solved

upcasting down casting object remains same but refernece changes

Posted on 2016-08-16
8
66 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

763 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