aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes out put of class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "out put of class" Watch "out put of class" New topic
Author

out put of class

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
can any body explain the out put as iam getting answer 2 ;
* but answer is 1;
public class Poly
{
public static void main(String argc[])
{
A ref1=new C();
B ref2=(B)ref1;
System.out.println(ref2.g());
}
}
class A
{
private int f() {return 0; }
public int g() {return 3; }
}
class B exends A {
private int f() { return 1;}
public int g() { return f(); }
}
class C extends B {
public int f() { return 2; }
}
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Originally posted by gouru:
can any body explain the out put as iam getting answer 2 ;
* but answer is 1;
public class Poly
{
public static void main(String argc[])
{
A ref1=new C();
B ref2=(B)ref1;
System.out.println(ref2.g());
}
}
class A
{
private int f() {return 0; }
public int g() {return 3; }
}
class B extends A {
private int f() { return 1;}
public int g() { return f(); }
}
class C extends B {
public int f() { return 2; }
}

I am using jdk1.2.2 and when this is ran, I get an output of 1. I thought the method that was chosen was based on the actual object? Could this result be because a B method made the call, it's method was chosen?

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi
the basic thing is that in method g() u r calling a private method f(). Remember that private methods are not overriden. thus class C method f() is not called.
if u change the B.f() method to public, protected, or default u will get the result that u expect.
if u have problems do tell me.
regds
Rahul.

[This message has been edited by rahul_mkar (edited June 21, 2000).]
 
Don't get me started about those stupid light bulbs.
 
subject: out put of class