Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Cant a class extends a abstract class and implements a interface at once??

Reply
Thread Tools

Cant a class extends a abstract class and implements a interface at once??

 
 
moxosyuri@gmail.com
Guest
Posts: n/a
 
      09-26-2006
Hi, i`m a beginner in java, and i found a strange question when i
learning "interface",
can anyone help me?!

just like :

public interface Shout
{ ...
void Shout();
...
}

class abstract Person { ... }

class Worker externs Person implements Shout
{ ...
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...

wanted your help!! thank you

 
Reply With Quote
 
 
 
 
opalpa@gmail.com opalinski from opalpaweb
Guest
Posts: n/a
 
      09-26-2006
It appears something else is going on. Here is an example (the class
relationship is taken from the last "Real Time with Bill Maher"):

package experiment;

abstract class misanthrope {
abstract Class dislikes();
}
interface bigot {
void intolerate(Class c);
}
class Woman {}


class misogynist extends misanthrope implements bigot {
Class dislikes() { return Woman.class; }
public void intolerate(Class c) {
System.out.println("I am intolerant of " + c);
}
}

class AbstractAndInterface {
static public void main(String args[]) {
misogynist m = new misogynist();
m.intolerate( m.dislikes() );
}
}

Your post contains mistakes like using the word "externs" for "extends"
and I'm pretty sure your compiler said more than "Cant find sign!" and
said it more correctly too.

Opalinski
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.geocities.com/opalpaweb/



(E-Mail Removed) wrote:
> Hi, i`m a beginner in java, and i found a strange question when i
> learning "interface",
> can anyone help me?!
>
> just like :
>
> public interface Shout
> { ...
> void Shout();
> ...
> }
>
> class abstract Person { ... }
>
> class Worker externs Person implements Shout
> { ...
> 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...
>
> wanted your help!! thank you


 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      09-26-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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 (E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
moxosyuri@gmail.com
Guest
Posts: n/a
 
      09-26-2006
thank you - Matt Humphrey and opalpa - and im very sorry about my
careless typing...*-*

i build another testfile and find everything works greatly, so i know
there must has some mistake in my older testfile, i`ll try my best to
find it out, godblessme...

P.S i blushed for my BADLY spelling, but THANK YOU to point it out,
thats good for me...
i`ll do better...

 
Reply With Quote
 
Javier
Guest
Posts: n/a
 
      09-26-2006

(E-Mail Removed) ha escrito:

> Hi, i`m a beginner in java, and i found a strange question when i
> learning "interface",
> can anyone help me?!
>
> just like :
>
> public interface Shout
> { ...
> void Shout();
> ...
> }


Constructors are not methods, they cannot be inherited (but you can
call them from subclases using "super"), and interfaces are not allowed
to have them just because they are not going to be called by "super"
ever. (It makes sense, "super" cannot call a constructor which has no
code).

 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      09-26-2006

Javier wrote:
> (E-Mail Removed) ha escrito:
>
> > Hi, i`m a beginner in java, and i found a strange question when i
> > learning "interface",
> > can anyone help me?!
> >
> > just like :
> >
> > public interface Shout
> > { ...
> > void Shout();
> > ...
> > }

>
> Constructors are not methods, they cannot be inherited (but you can
> call them from subclases using "super"), and interfaces are not allowed
> to have them just because they are not going to be called by "super"
> ever. (It makes sense, "super" cannot call a constructor which has no
> code).


But it is legal, if bad form, to have a method (like above, returning
void) with the same name as the interface.

 
Reply With Quote
 
Tim Hallwyl
Guest
Posts: n/a
 
      09-26-2006
(E-Mail Removed) wrote:
> public interface Shout
> { ...
> void Shout();
> ...
> }
>
> class abstract Person { ... }
>
> class Worker externs Person implements Shout
> { ...
> 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.


Note: In your code samples above it is not an abstract class that
implements the interface.

There should not be a problem in both extending a class and implementing
an interface. Make sure that all abstract methods and methods in the
interface are implemented. In your sample Worker must implement all
abstract methods in Person (if any) and methods in Shout.

> 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...


Why your compiler is complaining is difficult to say, not having the
exact code. But I hope this might give you a hint for the right direction.

--tim
 
Reply With Quote
 
Javier
Guest
Posts: n/a
 
      09-26-2006

Mark ha escrito:

> > Constructors are not methods, they cannot be inherited (but you can
> > call them from subclases using "super"), and interfaces are not allowed
> > to have them just because they are not going to be called by "super"
> > ever. (It makes sense, "super" cannot call a constructor which has no
> > code).

>
> But it is legal, if bad form, to have a method (like above, returning
> void) with the same name as the interface.


You're right, it returns void.

 
Reply With Quote
 
moxosyuri@gmail.com
Guest
Posts: n/a
 
      09-27-2006
well, i think ive found the resurlt at last.
....
public class Test
{
public static void main (String args []) {
Person[] man = new Person[2];
man[0] = new Worker ("Herry");
man[1] = new Worker ("Tony");
for(int i = 0; i < man.length; i++) {
Person p = man[i];
p.Shout(); // < - HERE!!
System.out.println(p.getName());
}
}
}

the jcreat said it cant find "Shout()" in " Person"
and the class Person didt implements Shout,
so "p.shout()" is a undefined method.

well, theres another question,
HOW does "Person p = main[i];" worked?
is "p" now a instance of Worker or still Person or neither?
im sure the answer is Worker, but WHY it report warning like that??

and whats more, i can execute file successfully while it still warning
theres a mistake...
so WHAT HAPPENED??


anyone help me?

 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using abstract class that implements interface Zuisman Moshe Java 24 04-21-2009 06:23 PM
Problem with implements and extends su_dang@hotmail.com Java 2 02-16-2007 08:58 PM
newbie question: Extends and implements Jacky Luk Java 4 11-23-2005 12:12 PM
How do I Implements Parent class interface in child class owais ASP .Net 3 06-23-2005 01:27 PM
How to create a java "extends"/"implements" clause with xsd file and castor? Stacey XML 0 01-20-2004 01:52 PM



Advertisments