Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sujeet Kumar Jaiswal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic