Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class Type Casting  RSS feed

 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can some one tell me about the Class type casting in JAVA... ???
i.e. what happens when we type cast a super class object to its sub class and type cast a sub class object to its super class..???
[ December 17, 2006: Message edited by: Aravind Jerubandi ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, you don't cast objects, only primitives and references. Casting a reference simply changes the static (compile time) type of that reference. Nothing happens to the object itself.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, try reading this and this. While these don't talk DIRECTLY about casting, they will help you understand this:

when you do casting, you are telling your remote that it should control a different TV. if i take my basic TV remote (reference), and tell it to control my superfancy TV (object), it will work, but since the basic remote doesn't have all the buttons, I won't be able to control my surround sound, my picture in picture, or change my input source - because that remote doesn't HAVE THE BUTTONS (methods) for those controls.

note that my TV did not change.

I cannot cast my Air conditioner remote to control my TV, no matter how hard I try, since those buttons don't make any sense for the TV.
 
Aravind Jerubandi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnx a lot for the reply...Let me put this question with a code example:
public class CastingExample{
public static void main(String args[]){

class1 obj1 = new class1();
class2 obj2 = new class2();
obj1 = obj2;
((class2)obj1).print2();
((class2)obj1).print();
((class1)obj2).print1();
((class1)obj2).print();
//((class1)obj2).print2(); //gives compiler error
//But why...???
}
}

class class1{
public void print(){
System.out.println("printing in Class 1");
}
public void print1(){
System.out.println("printing in Class 1");
}
}

class class2 extends class1{
public void print(){
System.out.println("printing in Class 2");
}

public void print2(){
System.out.println("2nd printing in Class 2");
}

}
And the output is :

2nd printing in Class 2
printing in Class 2
2nd printing in Class 1
printing in Class 2

Now can some one explain me the about this type casting. Is it like
1) when obj2 is type casted to Class1 then obj2 becomes the object of Class1 with the overridden methos of Class2
2) Similarly when obj1 is typecasted to Class1, then obj1 becomes the object of Class2 with the overridden methos of Class1

Please let me know if i am wrong...
And also how is it possible to typecast the super class object reference "obj1" to subclass type Class2...???

[ December 17, 2006: Message edited by: James ]

[ December 17, 2006: Message edited by: James ]
[ December 17, 2006: Message edited by: James ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

Welcome to JavaRanch!

We have a strict policy on display names, which must be a real first and last name with a space between. I'm pretty sure you started out with a valid display name, and now have changed it; please change it back.

Please go here and fix your display name up, pronto. Thanks, pardner!

Now, as far as your questions: please read and try to understand the answers you've already gotten, including the linked articles. Casting changes absolutely nothing about an object; all it does is tell the compiler what kind of object the variable will really be pointing to at runtime. If you lie, and get it wrong, there's a CLassCastException at runtime. If you lie so badly that the compiler can tell it's a lie, then your code won't even compile.

In the commented line that doesn't compile, you ask "why?" It's just because the cast tells the compiler to treat the variable as a class1, and class1 doesn't have a "print2" method. You're deliberately telling the compiler "this object has only the methods of class1", and then you're trying to call a method of class2 -- that's silly!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!