This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes fun with main() and abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "fun with main() and abstract" Watch "fun with main() and abstract" New topic
Author

fun with main() and abstract

Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Hi all,
Here's the source code stored in MainFun.java:

What's the result of trying to compile and run it:
A. Compiles and runs with "Can you inherit static methods?" output
B. Compiles, but complains at run time "java.lang.NoSuchMethodError: main"
C. Doesn't compile since main() is declared final in an abstract class
D. Doesn't compile since main() is not to be placed in abstract classes
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
Originally posted by Vad Fogel:
Hi all,
Here's the source code stored in MainFun.java:

What's the result of trying to compile and run it:
A. Compiles and runs with "Can you inherit static methods?" output
B. Compiles, but complains at run time "java.lang.NoSuchMethodError: main"
C. Doesn't compile since main() is declared final in an abstract class
D. Doesn't compile since main() is not to be placed in abstract classes[/QUOTE
----------------------------------------------------------------------------
Answer will be B
since we r saving the file as MainFun.java.So it will compile without error
No need to implement the method inside interface since it is non-absract.
So at runtime "no main method found "error will be displayed.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Nice try dhanashree, thanks, lets see if anybody else wants to guess. This is rather a rare question. Maybe, somebody would like to explain the concept of it.
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Well answer is
A. Compiles and runs with "Can you inherit static methods?" output
Since class MainFun is subclass of Fun, it inherits main method from Fun class. So for compiler won't complains as long as it gets to see same signature of main method.
What I'm not getting is how can Fun be declared as "abstract" when there is no abstract method in it??
Any ideas???
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Hi,
The answer is A.
1. MainFun inherits main() from Fun
a. if you tried to have a main() method in MainFun you would get a compiler error.
2. Although Fun is abstract there are no methods for MainFun to override.
Hence MainFun can be instantiated.
a. if you tried to instantiate Fun (Fun f = new Fun()) you would get a compiler error.
(1) and (2) is the minimum you need to run.
-Yoo-Jin
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
That is interesting Priyanka. However, any class can be declared abstract even if there are no abstract methods in it. Also, static methods are never inherited, they belong to class rather then an instance. So, to me, since MainFun is Fun, then main() is as good for it as for the parent class. I never tried to declare main() in MainFun, that would be even more fun!
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
Originally posted by Priyanka Chopda:
Well answer is
A. Compiles and runs with "Can you inherit static methods?" output
Since class MainFun is subclass of Fun, it inherits main method from Fun class. So for compiler won't complains as long as it gets to see same signature of main method.
What I'm not getting is how can Fun be declared as "abstract" when there is no abstract method in it??
Any ideas???


Hi priyanka,
It's not necessary that abstract class should contain abstract method.It can contan both abstract and non-abstract methods also.
I considered Fun as interface that was my mistake
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Hi,
I couldn't find anything in the JLS that says you cannot declare a class to be abstract if there are no abstract methods in it.
The closest I found was:
A class type should be declared abstract only if the intent is that subclasses can be created to complete the implementation. If the intent is simply to prevent instantiation of a class, the proper way to express this is to declare a constructor (�8.8.8) of no arguments, make it private, never invoke it, and declare no other constructors. A class of this form usually contains class methods and variables.
-Yoo-Jin
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Indeed, you can easily redeclare main() in MainFun, no sweat, just remember to remove final in Fun. It will run OK. So, what is it? It's not overriding, and I doubt if it's inheritance either.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
An abstract class does not have to have abstract methods. There are many examples of this from the JDK. The adapter classes in the java.awt.event package are declared abstract even though they have no abstract methods.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
static methods are inherited but they can not be overridden!
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Thanks Thomas. My question is: if static members are inherited, what happens when you redeclare exact same static method in a child class (e.g., you can redeclare main() in MainFun class of this discussion)? I agree static members can't be overriden, so what's the concept?
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Thanks all!
I've locked in my brain that
An abstract class does not have to have abstract methods.
-PC
leonardo battagli
Ranch Hand

Joined: Aug 28, 2003
Posts: 33
Originally posted by Yoo-Jin Lee:
Hi,
I couldn't find anything in the JLS that says you cannot declare a class to be abstract if there are no abstract methods in it.
The closest I found was:
A class type should be declared abstract only if the intent is that subclasses can be created to complete the implementation. If the intent is simply to prevent instantiation of a class, the proper way to express this is to declare a constructor (�8.8.8) of no arguments, make it private, never invoke it, and declare no other constructors. A class of this form usually contains class methods and variables.
-Yoo-Jin


An abstract class is a class that is incomplete, or to be considered incomplete. Only abstract classes may have abstract methods (�8.4.3.1, �9.4), that is, methods that are declared but not yet implemented
MAY HAVE
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Thomas, I just made a note about your comment that
static methods are inherited but they can not be overridden!
But in this example I see that method1 is static and is overriden!
Totally
Help!
-PC
----------------------------------------------------
class SuperClass{
public static void method1(){
System.out.println("super");
}
}
public class Sub extends SuperClass{
public static void method1(){
System.out.println("sub");
}
public static void main(String args[]){
SuperClass a = new Sub();
a.method1(); //1 prints Super
((Sub)a).method1(); //2 - prints Sub
}
}
-------------------------------------------------------
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
No, Priyanka, you don't override static members. Maybe, this can be of partial help.
JLS 8.4.6
I didn't see, though, how you can inherit static members. Anybody knows it?
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
well in this example I can see static method1 in super class is also present with same signature in sub class? Isn't this overriding? am I missing something here?
Can someone elaborate more on the following statement with respect to this example?
static methods are inherited but they can not be overridden!
Thanks in advance
-PC
------------------------------------------
class SuperClass{
public static void method1(){
System.out.println("super");
}
}
public class Sub extends SuperClass{
public static void method1(){
System.out.println("sub");
}
public static void main(String args[]){
SuperClass a = new Sub();
a.method1(); //1 prints Super
((Sub)a).method1(); //2 - prints Sub
}
}
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
static methods can not be overridden, they can only be hidden. Take a look at this:

If m() was overridden then this would print "B" but it doesn't, it prints "Superclass".
The point here is that static methods are not dynamically bound which is why they are not polymorphic. Static methods are always bound at compile time. Since they are not dynamaically bound they can't be overridden but can only be hidden.
[ August 28, 2003: Message edited by: Thomas Paul ]
Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
static methods r not overriden they r hiddn.
superclass a = new sub();
a.method1();
If method is ovreriden then at run time class of current object is checked but this not overriden so type of reference is checked which is superclass. So methos in superclass is invoked.
To invoke the method in sub class we have to do casting as follows
((sub)a).method1();
Unni Kainila
Ranch Hand

Joined: Jul 26, 2003
Posts: 45
Static members are inherited, but they cannot be overriden!
If you re-declare a static memeber in the subclass, you are infact "hiding" them. This is the same with instance variables.
In the SuperClass/Sub class example of Priyanka, watch the method in Sub class.
a.method1(); //1 prints Super
It prints "Super". If the method1() wasn't a static method, the result would have been "Sub", because you would have overriden the method1(). But in this case Supercalss:method1() was hidden by Sub:method1() in Sub class. So Polymorphism does not work here.
But watch your second line in main method:
((Sub)a).method1(); //2 - prints Sub
Since you casting the "a" to type Sub, it prints "Sub".
Hope the above make sense to you Priyanka.
Now for Vad Fogel. Here is an example how static memebers are inherited:


Now, again going back to the original question from Vad, I don't understand why it is a runtime error? I am sure it has nothing to do with the "final" declaration of main method. Any exceptional rules with the "static void main(String[] a)" method? I don't know. My guess was answer A.
I hope somebody can answer that...
Unni
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
The answer is A, I can't recall when I disagreed to that. Thanks all for the ideas on this discussion!
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Thanks a lot for replies!
I appreciate your help.
-PC
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Just one more observation with regards to static members before we're done: even though they can be inherited and can't be overriden as we got to know, try to put a final static method in a parent class and "hide " it with its copy in child. Guess what?
Unni Kainila
Ranch Hand

Joined: Jul 26, 2003
Posts: 45
Whether you "hide" it or "override" it, the rule of the game is same - as far as access modifier and qualifiers are conerned.
Well, try to make the same method "private" in the subclass.
It won't compile in either case.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You can't override or hide final methods.
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
I'm just trying to wrap up the overall fun with main(). Some may find it useful:

is a perfectly well-formed main() method. Also, you can overload main() infinitely, so say if you code

along with a valid main(), your program will be even better off (kidding, but it'll run).
main() can be inherited as we've seen, and also hidden by subclass' main().
Overall, it's just a method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: fun with main() and abstract
 
Similar Threads
some problems with terminology...
interface question
Top Level Class doubt
main() inside abstract class
Superinterface?