aspose file tools*
The moose likes Beginning Java and the fly likes Interfaces: Few Doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces: Few Doubts" Watch "Interfaces: Few Doubts" New topic
Author

Interfaces: Few Doubts

Sachin Dravid
Greenhorn

Joined: Jan 21, 2013
Posts: 12
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

Joined: Jun 22, 2005
Posts: 1527
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.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Joe Ridener
Ranch Hand

Joined: Jul 16, 2012
Posts: 86


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

Joined: Jul 16, 2012
Posts: 86
Sorry about the quotation error up there everyone.
Sachin Dravid
Greenhorn

Joined: Jan 21, 2013
Posts: 12
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

Joined: Mar 17, 2011
Posts: 8008
    
  22

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Sachin Dravid
Greenhorn

Joined: Jan 21, 2013
Posts: 12
Thanks a lot Winston. That was really helpful.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Sachin Dravid wrote:Thanks a lot Winston. That was really helpful.

You're most welcome.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces: Few Doubts