Results 1 to 15 of 15
  1. #1
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default Interface and abstract method

    I'm trying to create an interface called Player. The interface has an abstract method called play () that displays a message describing the meaning of "play" to the class. Create classes called Child, Musician, and Actor that all implement Player.

    I am stumped on how to start this script. I'm not looking for the answer, just an outline to help get me started. I am completely stumped.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cheesehead11 View Post
    I'm trying to create an interface called Player. The interface has an abstract method called play () that displays a message describing the meaning of "play" to the class.
    You understand that since this is an interface, you can't give the method any code, only a method signature. The best you can do is create the method signature and a thorough javadoc that states just what the method should do (but in no way constrains the method so that it has to do this). Does the play method return a String though? Perhaps a String that is the message that you refer to?


    Create classes called Child, Musician, and Actor that all implement Player.

    This is straightforward enough. Just create the three classes and give each of them a play() method.


    I am stumped on how to start this script. I'm not looking for the answer, just an outline to help get me started. I am completely stumped.
    What specifically is hanging you up at the moment?

  3. #3
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    If I'm understanding what the text book is teaching me the opening should look like this:

    public abstract class Player
    {
    private String name;
    public abstract void play()
    public String getName();
    {
    return name;
    }
    }
    public class Child extends Player
    {
    public void play()
    {
    System.out.println("I am a child and I am playing a game")
    }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    In your original post you stated that the instructions told you to create an interface, Player, not an abstract class. Please re-read the assignment instructions carefully and let us know precisely what the instructions state. If you're to create an interface, then you will need to get rid of the abstract class and instead create an interface.

  5. #5
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    That is where I am confused. The example in my text book starts off with an abstract class.

    I think it should be close to this:

    public interface Player
    {
    public abstract void play()
    }
    }
    public class Child extends Player
    {
    public void play()
    {
    System.out.println("I am a child and I am playing a game");
    }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't extend interfaces in a concrete class, rather you implement them. Please have a look at the tutorials on interfaces:

    Creating an Interface

  7. #7
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    I think i understand a little more. I don't know why I can't figure this out, i've been trying for days. I really want to learn this. I was able to do a javac and create a class. When i run child.java i get the message Child is not abstract and does not override abstract method in child.
    is this closer?

    public interface Player
    {
    public abstract void play();
    }
    public class Child implements Player
    {
    public void play()
    {
    System.out.println("I am a child and I am playing a game");
    }
    }
    Last edited by cheesehead11; 02-07-2011 at 05:40 AM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code looks OK to me, as long as the Player interface is in its own Player.java file and the Child class is in its own Child.java file.

  9. #9
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    Sweet, thanks so much. I do online schooling and they refuse to help me at all. For me java is hard to learn when your not physically going to class with a teacher.

    One last question, how do I end the program so it runs? The final file is called UsePlayer.java.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by cheesehead11 View Post
    Sweet, thanks so much. I do online schooling and they refuse to help me at all. For me java is hard to learn when your not physically going to class with a teacher.
    You're welcome.


    One last question, how do I end the program so it runs? The final file is called UsePlayer.java.
    For starters, give UsePlayer a main method, then in the main method declare a Child variable, initialize it, and call its method.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by cheesehead11 View Post
    Java Code:
    public interface Player
    {
    	public abstract void play();
    }
    Just one thing, that abstract is unecessary. All methods in an interface are abstract by definition.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Just one thing, that abstract is unecessary. All methods in an interface are abstract by definition.
    Just one other thing, that public is unecessary. All methods in an interface are public by definition.

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    cheesehead11 is offline Member
    Join Date
    Feb 2011
    Posts
    13
    Rep Power
    0

    Default

    So this is what I got: I know its not right

    public interface Player
    {
    private String name
    public void play();
    public String getName()
    {
    return name;
    }
    public void setName(String playerName)
    {
    name = playerName;
    }
    }
    public class Child implements Player
    {
    public void play();
    {
    System.out.println("game");
    }
    }
    public class Musician implements Player
    {
    public void play()
    {
    System.out.println("song");
    }
    }
    public class Actor implements Player
    {
    public void play()
    {
    System.out.println("part");
    }
    }
    public class UsePlayer
    {
    public static void main(String[] args)
    {
    Child myChild = new Child();
    Musician myMusician = new Musician();
    Actor myActor = new Actor();
    myChild.setName("I am a child");
    myMuscian.setName("I am a muscian");
    myActor.setName("I am an actor");
    System.out.println(myChild.getName() + "and I am playing a");
    myChild.play();
    System.out.println(myMusician.getName() + "and I am playing a");
    myMusician.play();
    System.out.println(myActor.getName() + "and I am playing a");
    myActor.play();
    }
    }

  14. #14
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Please use [code][/code] tags so we can read your code.

    Java Code:
    public interface Player {
        private String name
    
        public void play();
    
        public String getName() {
            return name;
        }
    
        public void setName(String playerName) {
            name = playerName;
        }
    }
    No.
    The others have already told you that interfaces do not have anything inside their method bodies.

    Java Code:
    public interface Player {
        public void play();
        public String getName();
        public void setName(String playerName);
    }
    It should be something more like that.

    Your syntax is a mess - you should really go over it.

    Java Code:
    public class Child implements Player {
        public void play();
        {
            System.out.println("game");
        }
    }
    You sure that semicolon should be there after play() ?

    You have spelling typos all over the place :
    Java Code:
    Musician myMusician = new Musician();
    //...
    myMuscian.setName("I am a muscian");
    You need to write your code one little part at a time, compile to see if it works, if not read errors carefully and look at code, fix it, then add more - not spit out a huge lump of code all at once.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    When you have a problem with your code don't just stick the code here. You need to tell us what the problem is (compilation errors etc) and what you are confused about.

Similar Threads

  1. Replies: 3
    Last Post: 08-13-2010, 05:57 AM
  2. Abstract & Interface
    By BharathII in forum New To Java
    Replies: 3
    Last Post: 07-20-2010, 04:08 PM
  3. Replies: 3
    Last Post: 09-16-2009, 10:27 PM
  4. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  5. Replies: 2
    Last Post: 11-21-2008, 01:20 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
  •