• 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

strange result

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A {
int a = f();
int f()
{
return 1;
}
}
class B extends A
{
int b = a;
int f()
{
return 2;
}
}
public class CtorDemo1
{
public static void main(String args[])
{
B bobj = new B();
System.out.println(bobj.b);
}
}
The output is 2. I think it should be 1. acn anybody help me in this.
Thanx
Monika
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
this is overriding ..
as the object is of the derived class ,f() in the base class is overriden.
thus
a = f() calls the f() of the derived class...
one more intresting thing
in the derived class
instead of return 2; write return b; and see the result..
HTH
anil
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See it's a pure OO funda. You have created an object of Class B.Now every time you call the overridden method f() using this object reference the method in Class B will be called and not that from A.

Originally posted by Monika Pasricha:
class A {
int a = f();
int f()
{
return 1;
}
}
class B extends A
{
int b = a;
int f()
{
return 2;
}
}
public class CtorDemo1
{
public static void main(String args[])
{
B bobj = new B();
System.out.println(bobj.b);
}
}
The output is 2. I think it should be 1. acn anybody help me in this.
Thanx
Monika


 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When bobj.b is referenced it goes to class B,
int b = a. Variable a is found in class A but because the reference is of type class B it will execute method f() from class B and not A. Just remember when you instantiate a class the methods of that class will be executed. e.g
A bobj = new B();
I hope that helps.
AH
 
Monika Pasricha
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx everybody. Now i got it.
Monika
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic