File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Interface and Implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface and Implementation" Watch "Interface and Implementation" New topic
Author

Interface and Implementation

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Hi, I have a package ABC. In that package, I have an interface, an implementation for the interface and a Driver that uses tests the application.

public interface Greetings {
public void sayHi();
public void sayBye();
}

public class Implementation {
public void sayHi() {
//do something
}

public void sayBye() {
//do something
}

public boolean check() {
//return a bolean value
}
}

public class Driver {
public static void main(String args[])
{
Greetings obj = new Impl();
boolean ret = obj.check(); ---------ERROR
obj.sayHi();
obj.sayBye();
}
}

The object obviously does not recognize the method check. How do I overcome this problem so that I could use check() in the Driver but not include it in my interface?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

There are two ways, either use you implementation directly:

or cast the object before using it:


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Thanks for your response. Solution 1 seems fine.
I would however like to use soultion 2. When I tried it, I got an error-
Cannot convert IMPL to type boolean. Is there any way, I can use solution 2 or something similar?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Lucky Singh:
Hi, I have a package ABC. In that package, I have an interface, an implementation for the interface and a Driver that uses tests the application.



The object obviously does not recognize the method check. How do I overcome this problem so that I could use check() in the Driver but not include it in my interface?


Simply using interface inheritance, or horribly using a cast to a concrete type.


Now your implementation should implement the CheckableGreetings interface (if that indeed what it should do). Note that your provided source code sample contains errors, so I won't try to fix them based on assumptions. I hope the point is clear. Casting can always be avoided with a more approopriate abstraction.
[ August 24, 2005: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
 
 
subject: Interface and Implementation