• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

please help me out.. exam tomorrow

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class parent
{
public static int i()
{
return 42;
}

}
class child extends parent

{
int i;

public static void main(String [] args)
{

parent c = new parent();
child t =(child)c;
System.out.println(t.i());


}

}



By theory the program shud rum n give de ouptput 42..but it s nt working..please help me out.give a gud explanation.

(not urgent, nothing is in this forun)
[ August 03, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static methods /fields are NOT inherited.
Thats really fundamental.

Wonder how you want to take your exam tomorrow. whatever.

Good luck!

By theory, the program will fail compilation.

mfg
Flom
[ August 03, 2006: Message edited by: Flom Xanther ]
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this in one file? What's the name of the Java File?
 
Amit Ghai
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're trying to type cast a superclass object to it's sub class type...!
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
harish pk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but can you just copy this program and run it in your editor??it compiles fine..
 
Flom Xanther
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mea culpa. is was wrong ..
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code will throw ClassCastException at runtime below is your code in which i have made Single change it will display output as 42
 
harish pk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Flom I know it for sure that static methods can be inherited.only they cannot be overridden to be non static..any way never mind any suggestons??
 
harish pk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks gowher for that
 
harish pk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually Mr Owen, the parent csst is unnecessary rite???
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code mentioned by you raise ClassCastException as Parent can't be typecasted to child class .

This version returns you 42

class parent
{
public static int i()
{
return 42;
}

}
class child extends parent

{
int i;

public static void main(String [] args)
{

parent c = new child();
child t =(child)c;
System.out.println(t.i());


}

}


Static methods can't be over riden as they are class level not object level . Hence the problem of your code was wrong typecasting not static over ridding
 
harish pk
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a ton buddy...
 
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic