Results 1 to 6 of 6
  1. #1
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default Overiding an abstract method

    I tried to override an abstract method in a child class but complier complains and does not allow me to do this. Is it a rule or I am making some mistake?

    Java Code:
    public class Jupiter
    {
    public static void extractUsers() {
    }
    }
    
    public class Moon extends Jupiter
    {
    public void extractUsers() {
    }
    }
    - PEACE

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Your code didn't compile for me either, but the following did:
    Java Code:
    public class Jupiter {
      public static void extractUsers() {
        System.out.println("Superclass output");
      }
    } 
    
    public class Moon extends Jupiter {
      public [B]static [/B]void extractUsers() {
        System.out.println("Subclass output");
      }
    }
    You can't override a static method as you did above, but the above code I posted does work. I'm not sure about your abstract method because I don't see one.

    EDIT: see here for great info on the matter.
    Last edited by CaptainMorgan; 01-02-2008 at 08:11 PM.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Abstract method

    Hello, eva.

    An abstract method can only be declared in an abstract class. Also, when you "override" an abstract method it is actually called implementing that method. Remember that an abstract method cannot do anything by itself.

    Java Code:
    public [COLOR="RoyalBlue"]abstract[/COLOR] class Jupiter {
      public [COLOR="RoyalBlue"]abstract[/COLOR] void extractUsers() {
        System.out.println("Superclass output");
      }
    }
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Quote Originally Posted by eva View Post
    I tried to override an abstract method in a child class but complier complains and does not allow me to do this. Is it a rule or I am making some mistake?

    Java Code:
    public class Jupiter
    {
    public static void extractUsers() {
    }
    }
    
    public class Moon extends Jupiter
    {
    public void extractUsers() {
    }
    }
    - PEACE
    Method in Moon is hiding the static in implementation that is illegal. Try again with static in the extract user .. hope it helps .. tim is right for the abstract thing ..
    dont worry newbie, we got you covered.

  5. #5
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default

    Hi everyone,

    Thanks for the explanation. I now realize that my code was horribly wrong. I learned a lot of things though.

    Chao.

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Quote Originally Posted by eva View Post
    Hi everyone,

    Thanks for the explanation. I now realize that my code was horribly wrong. I learned a lot of things though.

    Chao.

    Better to have tried and learned a lot, than not tried and learned nothing! :)

    Glad you figured it out.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  2. Can this be done using abstract class??
    By romina in forum New To Java
    Replies: 6
    Last Post: 03-02-2009, 01:46 PM
  3. Replies: 6
    Last Post: 10-27-2008, 01:25 AM
  4. Abstract Class
    By gapper in forum New To Java
    Replies: 4
    Last Post: 01-31-2008, 02:25 PM
  5. GUI for Abstract...
    By judepereira in forum AWT / Swing
    Replies: 0
    Last Post: 01-04-2008, 09:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •