my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes scjp exam problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "scjp exam problem" Watch "scjp exam problem" New topic
Author

scjp exam problem

PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
1. interface Beta {}
2.
3. class Alpha implements Beta {
4. String testIt() {
5. return �Tested�;
6. }
7. }
8.
9. public class Main1 {
10. static Beta getIt() {
11. return new Alpha();
12. }
13. public static void main( String[] args ) {
14. Beta b = getIt();
15. System.out.println( b.testIt() );
16. }
17. }
A. Tested
B. Compilation fails.
C. The code runs with no output.
D. An exception is thrown at runtime.
Answer: B
Why it compiles fails ??
Emilio Botella
Greenhorn

Joined: Sep 06, 2004
Posts: 6
The type (time compiler) of b is Beta interface so compiler is upset that there isn't any method called testIt() in Beta.

You should put in String testIt(); in Beta and implement it with public access in the class.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Beta is an interface that contains no methods. Alpha is a class that implements Beta, and contains a method called testIt().

The variable b is an instance of Alpha, but it is upcast to type Beta. From the compiler's perspective, b is of type Beta, so it contains no methods. Therefore, b.testIt() results in a compile-time error.

In order to call testIt() in object b, you could explicitly downcast it back to type Alpha...

Alpha a = (Alpha) b;
a.testIt();

Or, as Emilio suggested, you could add the method testIt() to the interface Beta. (Note that, since Beta is an interface, all methods are implicitly public and abstract. The compiler would be happy because the method exists in the declared type. And at runtime, polymorphism would invoke the method implementation in Alpha.)

[ September 12, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
interface Beta {
String testIt();
}

class Alpha implements Beta {
public String testIt() {
return "Tested";
}
}

public class Main1 {
static Beta getIt() {
return new Alpha();
}
public static void main( String[] args ) {
Beta b = getIt();
System.out.println( b.testIt() );
}
}
The answer is :Tested
Or:
interface Beta {
}

class Alpha implements Beta {
public String testIt() {
return "Tested";
}
}

public class Main1 {
static Beta getIt() {
return new Alpha();
}
public static void main( String[] args ) {
Beta b = getIt();
Alpha a=(Alpha) b;
System.out.println( a.testIt() );
}
}
The answer is also:Tested

Now thanks !!!
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
Keep in mind the point of an intrface is to declare that implementing classes will implement the methods. This is so polymorphic calls will work. Thus, when you see an interface with no methods you should already be questioning what is happening.
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
Tolman:
Thank you very much !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scjp exam problem
 
Similar Threads
compilation error
Please explain this
Why Compilation fails here???
Interface Q
GC question