Using Interfaces.
Hello all! Me again with a somewhat stupid question, but as JavaNewb (fellow classman) was asking in his topic, we need help with Interfaces.
I just really need a clarification that I couldn't right out find in the Java Tutorials.
Does an Interface work such that you declare class names (such as "public void characters") and then you can write the code, having that class with the same class name as in the interface, in another .java, implement the interface and then when you need to use that class you just do something like "Interface.className(value);" in a class that is also implementing that Interface? (Please say yes :(!)
Again, I thank everyone who even gives me the smallest amount of help in advance. You guys are really good at helping!
# 1 Re: Using Interfaces.
Does an Interface work such that you declare class names (such as "public void characters") and then you can write the code, having that class with the same class name as in the interface, in another .java, implement the interface and then when you need to use that class you just do something like "Interface.className(value);" in a class that is also implementing that Interface? (Please say yes :(!)
I'm not sure what you're describing here but the answer to your question is no. That is not how interfaces are used. I think of interfaces as a contract, and every class that implements the interface is contractually obligated to provide the methods signatures specified in the interface contract, and more importantly, provide the behavior specified in the interface documentation. An interface allows you to write code that interacts with classes that you haven't even thought of yet, in that respect they are quite powerful.
See also:
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
http://www.dev-archive.com/java/tij/tij0080.shtml
http://www.artima.com/designtechniques/interfaces.html
# 2 Re: Using Interfaces.
Ok, I'm not quite understanding something...
My interface goes as follows:
public interface totalRun {
public int combat(int charType, int monHP, int playHP, int playMP, int STR, int INT, int mSTR, int monDS, int monNR, int wepDS, int wepNR, int lvl, int monEXP)
;
public int Characters(int exp)
;
public int Monstats(int encounter)
;
public int converter()
;
public int diceRoll(int dieSide, int numRoll)
;
public int encounter(int lvl, String location)
;
}
Now I try to run my combatTest which is as follows:
abstract class combatTest implements totalRun {
public void main(String[] args) {
int exp;
int dummy;
exp = combat(1,99,99,0,6,5,10,10,2,10,2,15,300000);
/** The following was a test to see if I needed to fill in all variables of classes used.
dummy = Characters(1);
dummy = Monstats(1);
dummy = converter();
dummy = diceRoll(1,1);
dummy = encounter(1,"blank"); **/
//charType,monHP,playHP,playMP,STR,INT,mSTR,monDS,monNR,wepDS,wepNR,lvl,monEXP
System.out.print("\n" +"You gained " +exp +" exp.");
}
}
When I run this I get no errors in code, however:
java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.
does appear. Why will it not run? All of my classes under the interface are abstract, non-static classes that all need each other to run.
# 3 Re: Using Interfaces.
You need to read up on interfaces in greater detail. By themselves, interfaces don't "do" anything. They are just a contract that states that the classes implementing them will contain the methods listed in the interfaces.
So problems I see:
Your class that implements the interface needs to in fact implement them. It needs to have methods that have the same signature as the interface methods.
Your main method is in an abstract class. You can never run an abstract class and thus should never have your main method in this class.
Understanding of interfaces and abstract classes, where they are useful and what they can and can't do is very important if you want to fully understand the java language.
Again, I recommend that you read up on interfaces and abstract classes in your Java textbook.
# 4 Re: Using Interfaces.
The other point is why you're getting that error - you haven't declared the main method static. If you do this, that particular error will go away and you'll get others. If you fix the errors in the main(..) method, you will be able to run it, but you won't be able to do anything related to the combatTest or totalRun classes for the reasons Petes1234 has explained.
To be honest, there are too many errors of understanding in the code you posted to explain each one. You really need to take a course, or read a book or follow a tutorial on the Java language basics. You will take forever to get anywhere if you don't learn the basics first. You can't learn to write Java by trial and error.
We can help you with specific problems, but we don't teach Java here.
There does not now, nor will there ever exist, a programming language in which it is the least bit hard to write bad programs...
L. Flon
dlorde at 2007-11-10 2:18:26 >
