• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Marcus Mock Exam No 3 Question 34

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't understand the explanation of why a compile error occured after this program was compiled.
Any help is appreciated.

------------------
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No one is going to hop over to find and look at question 34. Copy and paste (question w/ its possible answers) it into your post.
That's what we do here on the Ranch!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And while you're at it - what exactly does the compile error say? And what line # does it refer to? This is all useful info, and greatly increases the chance of getting an answer here.
 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about not including the question below.
I just noticed that the class ExBase is not declared as abstract even though it's method martley is declared as abstract. If any of a classes methods are abstract that class must also be declared abstract. Therefore, this code would not compile.
Here was the question:
#34
What will happen when you attempt to compile and run the following code?
import java.io.*;
class ExBase{
abstract public void martley(){}
}
public class MyEx extends ExBase{
public static void main(String argv[]){
DataInputStream fi = new DataInputStream(System.in);
try{
fi.readChar();
}catch(IOException e){
System.exit(0);
}
finally {System.out.println("Doing finally");}
}
}

1) Compile time error
2) It will run, wait for a key press and then exit
3) It will run, wait for a keypress, print "Doing finally" then exit
4) At run and immediately exit
The answer they give is (1) Compile time error. It produces an error like "Abstract and native method can't have body".
Thanks.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

class ExBase{
abstract public void martley(){}
}

This method is abstract so it should not have body({}).. also the class should be declared abstract if it has one or more abstract methods...
Hope this helps..
Wali
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John,
Your doubt is logically correct. But the Java compiler does a sequence of tests. First it tests for the syntax errors. Then there is a 2nd set of tests which checks for the declarations. So in the 1st phase of test, it catches the abstract method NOT written according to the Java spec . Abstract methods can't have a body right?. So it complains that.
If you think in other way also, first we have to declare the abstract method declaration correct. Then only ,we can complain that the class should be declared abstract. Isn't?. I think the compiler is doing a correct work here.
Also note that when we re-write the above code with the correct abstract method declaration, as you said (expected), the compiler will throw a different error asking us to declare the containing class as 'abstract'. Does this info make sense to you?
<pre>

class Test {

//public abstract void mtd1() {}
//For this above code "Abstract methods can't have body" compiler error
//will occur
public abstract void mtd1();
//For this "class Test must be declared abstract" compiler error will
//occur
}

</pre>
regds
maha anna

[This message has been edited by maha anna (edited July 13, 2000).]
 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes... it makes sense. Thanks a lot.
The compiler first checks for syntax errors and then on the second pass will check that all declarations are valid.
So... this code is actually failing on the invalid abstract method syntax (containing body{}). When this is corrected and compiled again, we get the class declaration error (class with abstract methods must be declared abstract).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic