Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces + error message  RSS feed

 
Donovan Verrill
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
apologize, but I've searched and searched and have not found an answer to this question. I am at the very beginning stages of trying to learn java and am having difficuly getting past the very beginning of the sun java tutorial. Here is the code I am using, followed by the error message I am getting. If someone could enlighten me I'd greatly appreciate it. Thanks.

Code: Phone.java

class Phone implements PhoneMeth {
public static void main(String[] args) {

// Create two different Phone objects
PhoneMeth phone1 = new PhoneMeth();

// Invoke methods on those objects
phone1.GetDialTone();
phone1.DialNumber();
phone1.HangUp();
phone1.printStates();
}
}

Code: PhoneMeth.java

interface PhoneMeth {
void GetDialTone();
void DialNumber();
void HangUp();
}

Error message when trying to compile Phone.java (PhoneMeth.java compiles fine)

C:\Documents and Settings\verrilld\My Documents\Phone.java:1: Phone is not abstract and does not override abstract method HangUp() in PhoneMeth
class Phone implements PhoneMeth {
^
C:\Documents and Settings\verrilld\My Documents\Phone.java:5: PhoneMeth is abstract; cannot be instantiated
PhoneMeth phone1 = new PhoneMeth();
^
C:\Documents and Settings\verrilld\My Documents\Phone.java:11: cannot find symbol
symbol : method printStates()
location: interface PhoneMeth
phone1.printStates();
^
3 errors

Tool completed with exit code 1

Again, thanks for any help.

Donovan
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

First error: As you know, methods declared in an interface have no implementation. So when a "concrete" (non-abstract) class implements an interface, that class needs to provide implementation for those methods by overriding them. If the class does not provide implementation, then those methods are left abstract, and so the class itself must be declared abstract.

Second error: You cannot instantiate an interface because it's implicitly abstract. Instead, you can instantiate a concrete class that implements an interface, and then upcast the reference to the interface type. ( PhoneMeth phone1 = new Phone(); )

Third error: You're trying to call a method "printStates" that has not been defined (as far as I can see).
[ August 17, 2006: Message edited by: marc weber ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!