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

overriding

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/********plz observe this***************/
class abc{
abc()
{
method();
}
void method()
{
System.out.println("method in base class");
}
}
class xyz extends def
{
public static void main(String args[])
{
xyz ob=new xyz();
}
void method()
{
System.out.println("method in derived class");
}
}
here the output is
"method in derived class"
although the method is called from base class constructor
but the ovveridden version of method is called.
plz help me.
thanx in advznce
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
put this line in your xyz class' method()
System.out.println(this.getClass().getName());
As you probably know this is object's reference passed in hidden form (not explicitly) to allow to select which object has made call.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excuse me if i am wrong somewhere
the code u have written should be something like this
class abc
{
abc()
{
method();
}
void method()
{
System.out.println("method in base class");
}
}
class xyz extends abc
{
public static void main(String args[])
{
xyz ob=new xyz();
// xyz ob=new abc();

}
void method()
{
System.out.println("method in derived class");
}
}
try running the code then see the output.Also try removing the statement code in call xyz.
waiting for u reply

------------------
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic