• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces: Few Doubts

 
Sachin Dravid
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have some basic doubts related to interfaces. Consider the following example:





Now, my questions go as follows:

1) When does the compiler checks if the class implementing a particular interface has implemented the methods declared in that interface or not? Is it during compile time?

2) I am using eclipse IDE. I was trying to apply breakpoints in the interface, but eclipse was not allowing me to put breakpoint in an interface. What is the reason for it? Why I cannot apply any breakpoint in an interface?

3) Agreed that interfaces are the best way for a java program to interact with the outside world, as they form a kind of contract between the implementing class and outside world. But suppose the developer of class XYZ has written another method, say method_02() in the class XYZ, which he feels is useful for the outside world. And suppose he forgets to declare that method in the interface, then outside world will not be able to access that method, still his program will compile and run fine. Is there any way to catch such scenario, say during testing phase?
 
Akhilesh Trivedi
Ranch Hand
Posts: 1603
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Dravid wrote:

1) When does the compiler checks if the class implementing a particular interface has implemented the methods declared in that interface or not? Is it during compile time?

Yes. Try to not implement a method and compile and see what error you get.

Sachin Dravid wrote:
2) I am using eclipse IDE. I was trying to apply breakpoints in the interface, but eclipse was not allowing me to put breakpoint in an interface. What is the reason for it? Why I cannot apply any breakpoint in an interface?


One reason would be because interfaces are just declarations and not implementations.

 
Joe Ridener
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1) When does the compiler checks if the class implementing a particular interface has implemented the methods declared in that interface or not? Is it during compile time?







You can access the Java compiler that resides in your bin directory of the Java directory ( when you install the JDK a program in that directory says javac) via the command line. The editor that you are using is Eclipse and I do not know that much about the Eclipse. NetBeans keeps a running copy of a jar file; thus, the program is constantly being compiled as you work on it. Your editor (NetBeans or Eclipse) will usually alert you of any errors including an improperly implemented interface. If you run a program from the command line, to turn your .JAVA files into .class files, you access the compiler using the javac command followed by the file name.





3) Agreed that interfaces are the best way for a java program to interact with the outside world, as they form a kind of contract between the implementing class and outside world. But suppose the developer of class XYZ has written another method, say method_02() in the class XYZ, which he feels is useful for the outside world. And suppose he forgets to declare that method in the interface, then outside world will not be able to access that method, still his program will compile and run fine. Is there any way to catch such scenario, say during testing phase?



I cannot tell you for sure that your program would compile without implementing methods of the interface. I am not sure it is possible to compile a program that has not implemented the methods from the extending interface. First your editor will alert you that your implementation did not include overriding necessary methods so it has never made it to the compiler from me. The errors that are issued from your editor may very well be from attempted compilations that are ongoing ... this I do not know. If your asking.. if you put the method as though you were implementing but saved writing the code and ended up forgetting to. It would simply remain like that until you caught it. Errors like this can make programming difficult. Although a method with no code in the body is a little easier to catch then other snippets you leave out or forget.

 
Joe Ridener
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the quotation error up there everyone.
 
Sachin Dravid
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for the reply. But I am still not sure about the last 2 questions:

2) I am using eclipse IDE. I was trying to apply breakpoints in the interface, but eclipse was not allowing me to put breakpoint in an interface. What is the reason for it? Why I cannot apply any breakpoint in an interface?

3) Agreed that interfaces are the best way for a java program to interact with the outside world, as they form a kind of contract between the implementing class and outside world. But suppose the developer of class XYZ has written another method, say method_02() in the class XYZ, which he feels is useful for the outside world. And suppose he forgets to declare that method in the interface, then outside world will not be able to access that method, still his program will compile and run fine. Is there any way to catch such scenario, say during testing phase?


Thanks in advance.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Dravid wrote:Why I cannot apply any breakpoint in an interface?

Because there's no code in an interface, and breakpoints apply to code.

And suppose he forgets to declare that method in the interface, then outside world will not be able to access that method, still his program will compile and run fine.

Not if someone tries to use that method it won't.

Is there any way to catch such scenario, say during testing phase?

Yes, but I hope you'd be catching it long before testing.

Interfaces are part of the thought process (ie, the design) of an application, so you should already have a pretty good idea of what they're going to contain before you start writing any code.

And how do you make sure they're right? - Reviews - Talk with other people; show them what you've got; find someone who's done something similar, and pick his/her brains. Also: get a good book on design techniques/principles.

Programming is 80% thought, so if you find yourself doing different - especially if you're running into difficulties - StopCoding.

Winston
 
Sachin Dravid
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Winston. That was really helpful.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Dravid wrote:Thanks a lot Winston. That was really helpful.

You're most welcome.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic