Results 1 to 15 of 15
Thread: Interface and abstract method
- 02-06-2011, 11:44 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
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.
-
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.
What specifically is hanging you up at the moment?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.
- 02-07-2011, 03:39 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
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")
}
}
-
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.
- 02-07-2011, 03:55 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
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");
}
}
-
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
- 02-07-2011, 04:35 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
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 04:40 AM.
-
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.
- 02-07-2011, 04:55 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
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.
-
- 02-07-2011, 11:07 AM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 02-07-2011, 11:27 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 02-07-2011, 05:30 PM #13
Member
- Join Date
- Feb 2011
- Posts
- 13
- Rep Power
- 0
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();
}
}
- 02-07-2011, 09:07 PM #14
Please use [code][/code] tags so we can read your code.
No.Java Code:public interface Player { private String name public void play(); public String getName() { return name; } public void setName(String playerName) { name = playerName; } }
The others have already told you that interfaces do not have anything inside their method bodies.
It should be something more like that.Java Code:public interface Player { public void play(); public String getName(); public void setName(String playerName); }
Your syntax is a mess - you should really go over it.
You sure that semicolon should be there after play() ?Java Code:public class Child implements Player { public void play(); { System.out.println("game"); } }
You have spelling typos all over the place :
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.Java Code:Musician myMusician = new Musician(); //... myMuscian.setName("I am a muscian");
- 02-08-2011, 06:32 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
SkeletonMidlet is not abstract and does not override abstract method
By just_in_deed in forum CLDC and MIDPReplies: 3Last Post: 08-13-2010, 04:57 AM -
Abstract & Interface
By BharathII in forum New To JavaReplies: 3Last Post: 07-20-2010, 03:08 PM -
...is not abstract and does not override abstract method...
By Addez in forum New To JavaReplies: 3Last Post: 09-16-2009, 09:27 PM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 10:51 AM -
GameImpl is not abstract and does not override abstract method
By Macca07 in forum New To JavaReplies: 2Last Post: 11-21-2008, 12:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks