File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract class and static methods 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 "abstract class and static methods" Watch "abstract class and static methods" New topic
Author

abstract class and static methods

Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi all, here is a question:

Who would like to explain what's going on here and why?
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
Take a close look at where doStuff() is defined in Concrete. It is not a method of Concrete, but a method of an anonymous class stuffed into the static variable a. Now note that variable a has been redeclared in Concrete, so it is not the one declared in Abs (that one is hidden). Now when main() is executed, it invokes doStuff() on its copy of variable a, which contains an instance of Concrete. Since Concrete has no method doStuff() declared, it uses the one inherited from Abs. Hence the output.


The Inner that is named is not the true Inner.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Thanks Steve, but I think what's going on is the following:
main() gets inherited from Abs, therefore, it'll operate on the fields from the Abs class. Then, doStuff() is not overridden, it's again a simple inheritance, so polymorphism is not applicable. The overall picture is that inherited main() of Concrete calls inherited doStuff() to produce the output of E. However, if you place main() in Concrete, the answer will be StackOverflowError.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Regardless whether you run Abs or Concrete, you will get the same output, i.e.

The important thing to remember is that the variable a in main() is statically bound during compilation. So no matter where you call main()(Abs or Concrete), it will still use that variable defined in Abs.
However, the call to doStuff() in the main() method is dynamic. So, during run time, the VM will still have to resolve the object pointed to by a, which in this case is the instance of Concrete. Now since the class Concrete doesn't have any method called doStuff(), Java will invoke the one inherited from Abs.
Notice that if you define a doStuff() method in Concrete, it will call that method:

If you move the main() method to the Concrete class, then the variable a in Concrete will be statically bounded to main(). But still, the call to doStuff() will still be dynamic. And the doStuff() method it will call is the one defined in the anonymous class, because that is where the variable a is pointing to. But in this case, you will get a 'StackOverflow error' because the anonymous class' doStuff() is calling itself(because of this.doStuff).
[ September 20, 2003: Message edited by: Alton Hernandez ]
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Alton, thanks for this good explanation. The only confusing thing for me here is how early binding which is static a variable mapping still allows for polymorphic call to doStuff() method.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Vad Fogel:
Alton, thanks for this good explanation. The only confusing thing for me here is how early binding which is static a variable mapping still allows for polymorphic call to doStuff() method.

Hi Vad,
Variables are never polymorphic, but the calls to instance methods are:
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
I think it may be valid to say something like: it's what's on the right side of the dot that determines early or late binding. So in a.doStuff() it doesn't matter whether a is static or instance, only whether doStuff() is static or instance; being the latter, you get late binding.
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
I hasten to add that this works when the thing on the right is a method. Variables are always statically bound. Only instance methods are dynamic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class and static methods