• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface Question  RSS feed

 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am to I guess make a class called Student that implements Speaker interface


Student contains one instance data called String classRank. Now I think I did the class right but I am having a problem getting it to work. My first time working with interfaces.
I was to initialize the classRank in the constructor....now I think I did that part right, it is at the very end.


 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors don't have a return type and can return nothing. Remove the return line from the constructor.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It tells me I need to have some return type, unless I didn't set it up properly.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors should have the same name as the class

So it should be

 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I forgot the constructor has to have the same name as the class. Ok everything there checks out but for some reason it wont let me implement the methods to implement the speaker interface.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tarrell Fletcher wrote:but for some reason it wont let me implement the methods to implement the speaker interface.

You already have implemented the interface methods speak() and announce(). Note that classes marked abstract cannot be instantiated.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHen I go to the main method I type

but it tells me I cant instantiate type Student.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I just need to test it out to make sure it works. The implementation is created then, but I just want to test it out to see if it works.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should have a main method in your class to invoke and check how the implemented methods works - http://docs.oracle.com/javase/tutorial/getStarted/application/index.html#MAIN

You can check if you have implemented the methods correctly, by using an @Override before the method like below (if you are using Java 1.6 or later i think)

@Override
public String toString() {
return super.toString();
}
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found my mistake, I had spelled one of the methods wrong in interface and it didn't make the one in the abstract section.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome... the @Override annotation will tell if you are correctly overriding a method or not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!