aspose file tools*
The moose likes Java in General and the fly likes Interface problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interface problem" Watch "Interface problem" New topic
Author

Interface problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Given:
1. public class Electronic implements Device
{ public void doIt() { } }
2.
3. abstract class Phone1 extends Electronic { }
4.
5. abstract class Phone2 extends Electronic
{ public void doIt(int x) { } }
6.
7. class Phone3 extends Electronic implements Device
{ public void doStuff() { } }
8.
9. interface Device { public void doIt(); }

What is the result? (Choose all that apply.)
A. Compilation succeeds
B. Compilation fails with an error on line 1
C. Compilation fails with an error on line 3
D. Compilation fails with an error on line 5
E. Compilation fails with an error on line 7
F. Compilation fails with an error on line 9
Answer:
A is correct; all of these are legal declarations.




how can a class change parameter type of a method of interface or this is function overriding

i think answer should be c


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2247

Hum... what is the interface problem then?

Why there should be a compilation problem on line 3?

how can a class change parameter type of a method of interface


Well, it can't. The interface can just be implemented and its contract should be respected by the implementing classes. In your example, the Device interface is implemented by the Electronic class, so there should be no problems.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Sujeet Kumar Jaiswal
Greenhorn

Joined: Mar 07, 2010
Posts: 17
mohitkumar gupta wrote:how can a class change parameter type of a method of interface or this is function overriding[/b]




Well here its case of function overloading in Phone2 and since Electronic has already implemented the method of interface, Phone2 will compile. The fact is that all the interface methods must be implemented by the current class or any of class above in inheritance tree (super) which implements the interface. Once that is done, interface is happy and the class overloading methods has nothing to do with the interface happiness.
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

I'd like to add also that Phone1 and Phone2 are also both abstract classes therefore they do not need to implement the doIt() method however it would be required that any concrete classes extending those implemented that method.
[edit] oops didn't read the question right, ignore me... that method is already implemented in the Device class.

Sean


I love this place!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interface problem
 
Similar Threads
interface problem
Sierra/Bates Book Chapter 1 Question