Re: Cant a class extends a abstract class and implements a interface at once??
<moxosyuri@gmail.com> wrote in message
news:1159277254.026384.299850@d34g2000cwd.googlegroups.com...
Hi, i`m a beginner in java, and i found a strange question when i
learning "interface",
can anyone help me?!
The basic answer to your question is yes, a class may extend an abstract
class and implement an interface at the same time. That's how Java does
multiple inheritance. The real question is what's wrong with how you're
doing it.
just like :
public interface Shout
{ ...
void Shout();
Having a method with the same name as the interface is just a bad idea (my
compiler Eclipse under JDK 1.5.0_05 issued a warning, but it did compile).
Use "shout" instead.
...
}
class abstract Person { ... }
class Worker externs Person implements Shout
I know you mean "extends" here, but it's better for us all if you copy/paste
code exactly because otherwise I have no idea of what you've typed is really
what the compiler sees.
{ ...
public void Shout() { System.out.println(...);}
...
}
main
{ ...
Worker man = new Worker(...);
man.Shout();
...
}
// when compliing, the jcreater whill warning "Cant find sign!" and
//point at the line "man.Shout();"
and i find if dont use "extends (a abstract class)"
or just try "abstract class XXX implements XXX" are all OK.
SO i suppose that MAYBE we cant use both of them at once in java...
Is that TRUE?? I DONT KNOW and wish someone can tell me what happened
in fact...
There is something wrong in your real code--I can't tell what because
there's too much left out. I've never heard of the "Can't find sign"
message, although "Can't find symbol" is likely. It may have to do with
Worker not being able to see the Person declaration (no package import.)
Here is some sample code that does compile to show a class that extends and
implements. You wouldn't in real life put all your classes into one file
like this--it's just to show it's ok.
public class Test2 {
public static interface Shout {
void shout ();
}
public static abstract class Person {
}
public static class Worker extends Person implements Shout {
public void shout () { }
}
public static final void main (String args []) {
Worker man = new Worker ();
man.shout();
}
}
You will have to explain more about your packages and your actual code.
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/