Originally posted by James Baud:
Can anyone explain why this prints 1 if f() is private in PolyB, else if public it prints 2?
[/B]
James, This is my take on it -- and I could be wrong. But it's a good basic OOP question.
ref1 is a reference to a PolyC object. ref2 is also a reference to the same PolyC object, but it's being cast to a PolyB. If PolyB.f() is private, that means that PolyC.f() does NOT override PolyB.f() with it's own code since private methods are only accessible inside that class. Thus, when PolyB.f() is private and you call PolyB.f() (however indirectly), it's going to print PolyB.f()'s code. When PolyB.f() is public, it is overridden by the PolyC.f() method when the PolyC object is created. I altered your code to print strings out, which helped me make more sense of it. Take a look:
Everyone, if I'm wrong, let me know!