Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2010, 05:23 PM   PM User | #1
yotamoo
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
yotamoo is an unknown quantity at this point
Question error while trying to compile an interface

hello all
i'm trying to learn about interfaces. my understanding is that an interface forces the class implementing it to call all the method variables defined in it. am i right about the basic idea?

this is a little code i've written just to see if i got it right and i can't compile it for a few reason. i have two .java files: myInterface and myClass:

PHP Code:
// The interface
public interface myInterface {
    public 
void printWord (String word);

PHP Code:
//Class implementing it 
class myClass implements myInterface {

    public static 
void main (String[] args) {
    
String word="Hello World";
    
printWord(word);
    }

    public 
void printWord(String word) {
    
System.out.println(word);
    }

The error i keep getting:
PHP Code:
myClass.java:5non-static method printWord(java.lang.Stringcannot be referenced from a static context
    printWord
(word);
    ^
1 error 
so first - how do i make it right.
second - can the interface, class implementing it and the method all be in the same file?

thank you!
yotamoo is offline   Reply With Quote
Old 09-24-2010, 06:54 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Your implementation is correct, but your usage is wrong.
printWord is not static. You must call it from an instance of myClass:
PHP Code:
public static void main(String[] argv)
{
    
String word "hello world";
    
myClass mc = new myClass();
    
mc.printWord(word);

An interface is a contract. It guarantees that an Object of type myInterface will have a method called printWord implemented. Its up to the class how to do this, it could very well be nothing more than an exception toss. This is done to emulate multiple inheritance through polymorphism since Java does not natively allow extensions from multiple parents. Interfaces are at least 100x more useful than an extends anyway, so that really doesn't matter.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:38 AM.


Advertisement
Log in to turn off these ads.