• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

scjp exam problem

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ??
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
PETER CARTER
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!!
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tolman:
Thank you very much !!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic