• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
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
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic