• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Question 58 jiris on casting

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test058 extends Super
{
public static void main(String args[]) {
Test058 t = new Test058();
Super s = (Super)t;
s.method('a');
}
public void method(int i){System.out.println("base int");}
public void method(char c){System.out.println("base char");}
}
class Super
{
public void method(int i){System.out.println("super int");}
public void method(char c){System.out.println("super char");}
}
-----------------------------------------------------------------------
The output is: base char
I'm wondering why casting the variable t to Super doesn't call the the method of the Super class ?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting is only used on object references. The runtime type of the object is unaffected, and dynamic method lookup is used.
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Casting & object reference are two different things.Casting means converting an object reference from one class type to another.It doesn't change object reference.
Statement Super s = (Super)t is same as Super s = t
Complier will do implicit assignment conversion.But s holds object reference value of t.
Somebody correct me if I am wrong.
Veena
reply
    Bookmark Topic Watch Topic
  • New Topic