File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question from MindQ

 
Kishore Pamu
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the classes defined below:
import java.io.*;
class Super
{
int methodOne( int a, long b ) throws IOException
{ // code that performs some calculations
}
float methodTwo( char a, int b )
{ // code that performs other calculations
}
}
public class Sub extends Super
{
}
Which of the following are legal method declarations to add to the class Sub? Assume that each method is the only one being added.
a) public static void main( String args[] ){}
b) float methodTwo(){}
c) long methodOne( int c, long d ){}
d) int methodOne( int c, long d ) throws ArithmeticException{}
e) int methodOne( int c, long d ) throws FileNotFoundException{}
i want to know the answer for this question.
i think it is b,d,e.but iam confused .
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think is :
a) public static void main( String args[] ){}
you can have two or more classes in the same FILE (file should have the same name of one of those), all classes can have the main method, but JVM will access main whose have the same NAME as a FILE. since main() is static, you can access each main class using nameClass.main(args);
b) float methodTwo(){} //it's overloading

d) int methodOne( int c, long d ) throws ArithmeticException{}
it's a RuntimeException
e) int methodOne( int c, long d ) throws FileNotFoundException{}
it's a subclass of IOException

stevie
 
Pam Doucette
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't understand why a) wouldn't be included as a legal method. The Sub class is the public class - so shouldn't it be the one that is the same as the file name and contain the main method? The main method declaration looks right to me, too.
 
Kishore Pamu
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!stevie,
thanks for ur explanation
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pam,
The <code>main()</code> method is legal; it will not cause a compile error if it's included in the Sub class.
You can use main() in any class; wether it's public or not. It's handled like any other method; just has the special status of being the first one the JVM looks for when you run a class.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic