Downcasting objects in Java - java

class A{
}
public class Demo
{
public static void main(String s[])
{
Object o=(Object) new Demo();
if (((A)(o)) instanceof Object)
{
System.out.println("true");
}
}
}
I am getting Exception while running the class Demo.java:
java.lang.ClassCastException: Demo cannot be cast to A
How to downcast o reference to class A?

You can do that only if Demo extends A, otherwise, you simply cannot cast a class object to any other type.
public class Demo extends A {

Let's start from the beginning: This is terrible code.
That being said:
You are casting Demo to Object (for whatever reason, since in Java everything is Object, no need to cast).
You are then casting o, that you know it's of type Demo, to A (why would this work?).
You are checking if Object o is of type Object (why would this fail?)
Some notes:
o should not be viewed as a reference, it is an instance of Object, as you declared it. Forget how things worked in C.
Consider interfaces and if you want A to be an interface that Demo implements.
You can only cast instances to a class that they already extend.
Downcast example:
public class A {
int variable = 0;
}
public class Demo extends A{
}
public void testDowncast(){
Demo myClass = new Demo();
myClass.variable = 2;
A morphingTime = myClass;
System.out.println("And now Power Ranger Demo has turned into Mighty A:");
System.out.println("I am: "+morphingTime.getClass() + " and my variable is: " + morphingTime.variable);
}

The answer by R J is right
you can do that only if Demo extends A
For your information, you do not need to type cast any object while assigning to Object
Object o= new Demo();
and every object will always be instanceof Object i.e. your condition instanceof Object for class objects will always be true
And why are you trying to do the things this way, ((A)(o)) without checking the type with instanceof rather it should be,
if (o instanceof A)

First of all, you'r getting 'ClassCastException' because your actual object 'o' is of type class 'Demo' and classes 'Demo' and 'A' are not in the same inheritance tree. You didn't get compile error only because you have cast your object to class 'Object' (since 'A' and 'Object' are in the same inheritance tree). To resolve you situation you should change you code such that make both of them ('Demo and 'A') to be part of the same inheritance tree. For example you can extend Demo from A. Then, check the object 'o' without cast like this
if (o instanceof A) {
// now cast to 'A'
// and invoke any accessible method (or etc.) that class A provides
((A)o).doSomthingMathod();
}

You should simply rewrite your code as
Object o=new Demo();
if (o instanceof A)
{
System.out.println("true");
}
and then see what changes if Demo extends A

First thing u do not need to cast your Demo instance to object becuase without casting you can assign the Demo reference to Object class because Object is super class of all java classes.
public class Demo extends com.A {
}

Downcasting is the act of casting a reference of a base class to one of its derived classes.
For example :
public class Parent{}
public class Child extends Parent{}
public static void main(String args[]){
Parent parent = new Child(); // Parent is parent class of Child, parent variable holding value of type Child
Child child = (Child)parent; // This is possible since parent object is currently holding value of Child class
}
You can refer this question to get the answer.

Related

Java passing generic objects

I have different objects(Object A and object B). But some of the objects' fields are same. I can't change the object classes( i mean i cant write a implement/extends condition for them). I want to pass the objects to a method which uses the objects' fields They have same fields. I don't want to overloading. Which design is the most suitable for this?.
A obj1 = new A();
B obj2 = new B();
update(obj1);
update(obj2);
// my function
public <T extends myInterface> void update(T obj)
{
obj.field+=1;
}
public interface myInterface{
int field=0;
}
--------------
public class A{
int field;
.... // smt else
}
--------------
public class B{
int field;
.... // smt else
}
If you have two classes which do not implement a common interface or share a common base class, you can't really pass them to your function.
The fact that they have a common field doesn't matter.
You have 3 workarounds, none of which is really good:
Have your function accept Object type, and check its type (A or B) inside using instanceof. This is ugly and not recommended as any class can be passed inside, and also your code has to check it's type all the time.
Have your function accept Object type, Use reflection to access field with specific name, in this way:
Field field = obj.getClass().getDeclaredField('myfeild');
Object value = field.get(obj);
This is better in that your code doesn't have to check types, but more unsafe. Any class can be passed to your function, and there's some dark magic which relies on static field names. If field name changes, your code breaks.
Perhaps the best - Implement a wrapper for your objects. It will have two constructors, one for class A and one for class B. The wrapper will remember which kind of object resides inside. In its getField function if will have a single if statement. Have your function accept the wrapper type as the argument.
instanceof can be used indentify class of object. Like this:
public <T extends myInterface> void update(Object obj)
{
if ( obj instanceof A )
{
A a = (A)obj;
a.field+=1;
}
if( obj instanceof B )
{
B b = (B)obj;
b.field+=1;
}
}

why can not access child fields using parent reference

class A {
int super_var = 1;
}
class B extends A {
int sub_var = 2;
}
public class Demo{
public static void main(String []args){
A a = new B();
System.out.print(a.sub_var); //compile error
}
}
why this will end with a compile error ? reference (a) referencing to an Object of B it has sub_var so why is it restricted ? why reference (a) can access only the fields in A ?
Let's say you have these classes:
public class Animal() {
// ...
}
public class Fish extends Animal() {
public void swim() {...}
}
If you declared an Animal:
Animal x = new Fish();
and you called the swim() method
x.swim();
Would you expect it to work? I don't think so, because not every animal can swim. That's why you have to explicitly specify that the animal x is a Fish:
((Fish) x).swim();
In your case, if you wanted to call that method, you should specify (technically, it's called cast) the type:
System.out.print(((B)a).sub_var);
Note:
This works similar for methods and variables. I used a method in the example since it's more illustrative.
Edit:
Let's see this example:
Animal x;
if (some_condition)
x = new Fish();
else
x = new Cat();
x.swim();
This restriction exists, because Java won't know if, at execution time, the object assigned to x will have the method swim(). So to avoid this, you have to cast to the respective type to call a method that doesn't exist in superclass.
At first it does sound like it should work. (And in some languages it probably does.) But think about this example:
public class Demo {
public static void main(String []args){
A a = new B();
print( a );
}
public static void print( A arg ) {
System.out.print(arg.sub_var); //compile error
}
}
This functionally does the same thing but the print is in another method. If your version worked, this one could be expected to work too.
But what if someone then does this?
Demo.print( new A() );
This should fail because A doesn't have a sub_var. It would have to throw some kind of runtime error instead.
So the design decision in Java was not to allow this and if you declare a local variable/field/method parameter as type A, then you can only access things that every object that is either A or a subclass is guaranteed to have.
If you want to access more, you need to cast it to the subclass, which will throw an exception if you try it on an object that doesn't fit.
A a = new A();
System.out.print(((B)a).sub_var); //ClassCastException is thrown here
You can not access B members with the reference of Parent object A.
Instead change your println statement like below to access,
System.out.print(((B)a).sub_var);
Is there a variable called sub_var in the parent class ? No. That is why you get the error -
sub_var cannot be resolved or is not a field
See this
System.out.print(a.super_var); //okay
System.out.print(a.sub_var); //compile error
you create an object of type B and assign it to a variable of type A. The type A does not declare sub_var. This field is declared only in type B. the compiler only sees what is declared in type A, although the variable is instantiated to an object of type B.
If you want to access sub_var you would have to cast a to B.
System.out.println( ((B)a).sub_var);
The sub_var is in class B, so you can only access through a reference of class B. To the compiler A a = new B(); means a is an instance of class A.

Why Java object class remains same after casting?

I tried to upcast an objet. But at runtime object class is remained as a derived class.
Derived drv = new Derived();
Base base = (Base) drv;
System.out.println("Class : " + base.getClass());
//prints -> Class : class packagename.Derived
So Why class property didn't change?
So Why class property didn't change?
Because the object hasn't changed, just the type of the reference you have to it. Casting has no effect at all on the object itself.
In Java, unlike some other languages (thankfully), the type of the reference largely doesn't affect which version of a method you get. For instance, consider these two classes (courtesy of 2rs2ts — thank you!):
class Base {
public Base() {}
public void foo() {
System.out.println("I'm the base!");
}
}
class Child extends Base {
public Child() {}
public void foo() {
System.out.println("I'm the child!");
}
}
This code:
Child x = new Child();
Base y = (Base) x;
y.foo();
...outputs
I'm the child!
because even though the type of y is Base, the object that we're calling foo on is a Child, and so Child#foo gets called. Here (again courtesy of 2rs2ts) is an example on ideone to play with.
The fact that we get Child#foo despite going through a Base reference is crucial to polymorphism.
Now, it just so happens that the method you were calling (getClass) can only be Object#getClass, because it's a final method (subclasses cannot override it). But the concept is crucial and I figured it was probably the core of what you were asking about.
The chief thing that the type of the reference does is determine what aspects of an object you're allowed to access. For instance, suppose we add bar to Child:
class Child extends Base {
public Child() {}
public void foo() {
System.out.println("I'm the child!");
}
public void bar() {
System.out.println("I'm Child#bar");
}
}
This code won't compile:
Child x = new Child();
Base y = (Base) x;
y.bar(); // <=== Compilation error
...because Base has no bar method, and so we can't access the object's bar method through a reference with type Base.
You can not change the type of an instance in Java. All you're doing with your cast is reference it from a variable of a different type.
An upcast does not change the object's type. As a matter of fact, NOTHING changes a Java object's type.
That's the very core of OO programming: An object has a defined behavior that can't be influenced from the outside.

How can I cast base to a drived class?

Is there a work around that will allow me to cast an object of the base class to an object of the derived class?
something like the following
B extends A
A a = new A();
B b = (B)a
Is there a trick that will achieve this?
No, absolutely not. What would you expect the values of any fields declared in B but not in A to be? For example, what would you expect this to do:
Object x = new Object();
String text = (String) x;
System.out.println(text);
An Object has no text data... so what would it mean to cast it as a string?
You can only cast a reference to a type which is appropriate for the actual type of the object.
The desire to do this usually indicates a design problem somewhere... or it might mean that you want something like:
public class A {
public A() {
// Whatever
}
public A(A a) {
// Use the existing values in "a" to initialize this object
}
}
public class B extends A {
/** Creates a new B from the values in an A, with suitable defaults. */
public B(A a) {
super(a);
// Now initialize any fields in B with appropriate values
}
}
Then:
A a = new A();
B b = new B(a);
That will create two objects, unlike a cast... but it would at least be valid.
How is that even possible? Think about it. It is like saying if you have a class FourWheeler, you can simply cast it into a Ferrari and make it a Ferrari!
No, this isn't possible. When B extends A it inherits the behavior of A, but on the same time, there is nothing stopping you from defining new behavior for B (where those new behaviors won't be part of A)
For example say A has a single method called 'methodA'. Now when B extends A it inherits 'methodA' but it also declares another method called 'methodB'. So under such circumstance you will get a runtime 'ClassCastException' when you try to call the 'methodB' over an instance of Object A.

Java - Comparing classes?

How can i compare 2 classes?
The following if statement never passes although class is type of MyClass:
public void(Class class) {
if (class == MyClass.class){
}
}
if (clazz.equals(MyClass.class)) {
}
BTW, class is a reserved word.
To test whether clazz is a (sub) type of MyClass do
MyClass.class.isAssignableFrom(clazz)
From the javadoc for Class.isAssignableFrom
Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.
Specifically, this method tests whether the type represented by the specified Class parameter can be converted to the type represented by this Class object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.
So
Object.class.isAssignableFrom(String.class)
is true because each String is also an Object but
String.class.isAssignableFrom(Object.class)
is false because not all Objects are Strings.
The name "isAssignableFrom" comes from the fact that,
Class1 x = (Class2) null;
is only legal when
Class1.class.isAssignableFrom(Class2.class)
I.e., we can assign a field or variable with static type Class1 a value that comes from an expression whose static type is Class2.
You can use == or .equals() to compare Class objects.
Example:
class MyClass
{
public static void main (String[] args) throws java.lang.Exception
{
MyClass m = new MyClass();
if (MyClass.class == m.getClass())
{
System.out.println("it worked");
}
}
}
Demo: http://ideone.com/AwbNT
You can use instanceof operator to check if an instance belongs to a specific class or its subclasses.
class MyClass{}
class SubClass extends MyClass{}
public static void main(String args[]) {
SubClass object = new SubClass();
if (object instanceof MyClass) {
System.out.println("It works, too");
}
}

Categories

Resources