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

Discussion in 'Java' started by moxosyuri@gmail.com, Sep 26, 2006.

  1. Guest

    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:)
     
    , Sep 26, 2006
    #1
    1. Advertising

  2. 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.geocities.com/opalpaweb/



    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:)
     
    opalinski from opalpaweb, Sep 26, 2006
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > 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 http://www.iviz.com/
     
    Matt Humphrey, Sep 26, 2006
    #3
  4. Guest

    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...:)
     
    , Sep 26, 2006
    #4
  5. Javier Guest

    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).
     
    Javier, Sep 26, 2006
    #5
  6. Mark Guest

    Javier wrote:
    > 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.
     
    Mark, Sep 26, 2006
    #6
  7. Tim Hallwyl Guest

    Re: Cant a class extends a abstract class and implements a interfaceat once??

    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
     
    Tim Hallwyl, Sep 26, 2006
    #7
  8. Javier Guest

    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.
     
    Javier, Sep 26, 2006
    #8
  9. Guest

    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;
    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;" 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?
     
    , Sep 27, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacky Luk
    Replies:
    4
    Views:
    870
    Bhupendra
    Nov 23, 2005
  2. Stacey
    Replies:
    0
    Views:
    2,176
    Stacey
    Jan 20, 2004
  3. Replies:
    2
    Views:
    920
    F. Marker
    Feb 16, 2007
  4. Nagaraj
    Replies:
    1
    Views:
    892
    Lionel B
    Mar 1, 2007
  5. Zuisman Moshe
    Replies:
    24
    Views:
    3,013
    Zuisman Moshe
    Apr 21, 2009
Loading...

Share This Page