• Post Reply Bookmark Topic Watch Topic
  • New Topic

implements question  RSS feed

 
Abigail Decan
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm getting cryptic error messages for the following code:



it's telling me something's wrong with the class modifier and that my class can't implement charAt
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exact error message? Error messages most of the time contain very precise information that explains what is wrong and where exactly in your source code it's wrong, so it's important to pay close attention at the error message.

Looking at the API documentation of interface CharSequence, I see that it declares four methods. You have to implement all of those methods, if your class is not an abstract class. (Actually, you don't have to implement toString(), because your class already inherits toString() from class Object).

To answer your other question: Yes, a non-abstract class must implement all the methods of the interfaces that it implements. You cannot have a non-abstract class that has some methods without an implementation.
 
James Harte
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the error - You need to implement all necessary methods, in this case it looks like you are missing subSequence().

See http://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html#subSequence%28int,%20int%29

- Jim

C:\Program Files (x86)\Java\jdk1.7.0_06\bin>javac C:\Users\JamesHarte\Temp\MyChar.java
C:\Users\JamesHarte\Temp\MyChar.java:1: error: MyChar is not abstract and does not override abstract method subSequence(int,int) in CharSequence
class MyChar implements CharSequence{
^
1 error

C:\Program Files (x86)\Java\jdk1.7.0_06\bin>type C:\Users\JamesHarte\Temp\MyChar.java
class MyChar implements CharSequence{
private String s;

public MyChar(String input)
{
StringBuilder b = new StringBuilder(input);
b.reverse();
this.s = b.toString();
}

public char charAt(int index)
{
return s.charAt(index);
}

private static void main(String[] args)
{
MyChar m = new MyChar("abcde");
System.out.println(m.charAt(0));
}

}
C:\Program Files (x86)\Java\jdk1.7.0_06\bin>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!