Java - cant understand error message - java

I'm new at Java and I have started out with a tutorial... The tutorial wants you to start this way:
package proj;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Proj extends JFrame implements ActionListener {
public static void main(String[] args) {
}
}
But I get an error message on the class Proj, that is saying: "Proj is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener".
I don't quite understand what this error message means, and in the tutorial this isn't mentioned.

Your class Proj implements an interface. An interface can "tell" which methods a class, implementing this interface, has to implement. The ActionListener interface guarantees that a class implementing this interface has also to implement said method actionPerformed(...). So in order to fix this: Implement said method.
No offense, but: You might want to take a look into some Java starter tutorials or books.

ActionListener is an Interface containing the method actionPerformed(ActionEvent e). When you implement an interface, you agree to create an implementation of all methods declared in said interface.
You have not implemented the actionPerformed(ActionEvent e) method that's in the interface you told the Java compiler you would implement, hence the error. You also have to make sure to use the same modifiers for the method as in the interface, in this case public void.
I suspect the tutorial you are following will get to this, you probably just tried to compile the program prematurely. If not, I'd send an email to the owner of the tutorial!

If a class implements a interface, it should implement all the method in that interface, unless it's an abstract class
ActionListener is an interface in Java, and your class Proj is not an abstract class, so You must implement actionPerformed(ActionEvent e) method appropriately defined in that interface.

You need to override the actionPerformed method.

ActionListener is a interface. Interfaces declares methods, but don't include its body. When you implement a interface you should provide a body for inherited method or declare it as abstract (saying: hey! my subclass should implement it).

Just add the missing method for ActionListner interface.

Related

Is the abstract method in the interface Inter is same with the abstract method in the abstract class Test?

When I declare the 'abstract public void show();' in the abstract class Test does that create a brand new show() method or just refer to the show() method declared in the interface Inter? Please clarify.
interface Inter
{
void show();
}
abstract class Test implements Inter
{
abstract public void show(); //What does this line signify?
}
As you might have tested out already, removing the declaration in the abstract class produces no error. It can be safely removed.
If I were to speculate the reasons for this redundant line of code, one reason would be making it easier for subclasses of Test to implement the methods required.
Imagine you are trying to write a Test subclass and the line in question were not there. You go to the definition of Test to find what methods to implement, but you find nothing. You'd have to fo to Inter to see what methods you need to implement. Now imagine the chain of inheritance going much deeper. Do you see how many layers of classes you have to look through to see what methods to implement?
However, these kind of problems can be avoided by using a modern IDE like IntelliJ. It tells you what methods you need to implement automatically.
When I declare the 'abstract public void show();' in the abstract class Test does that create a brand new show() method or just refer to the show() method declared in the interface Inter? Please clarify.
That does not create a new method (no hiding). It overrides the method declared in Inter. Here's a screenshot of IntelliJ:
The little "O" with an upwards arrow indicates overriding.
Explicitly placing an abstract show method in the class has no functional effect - any concrete class that extends this abstract class will have to implement show() anyway, as it's defined in an interface the class implements.
Some coding conventions encourage listing such methods to make their existence more obvious, but it's a matter of taste mostly.

interface and overriding the methods of the interface

I'm 13 and quite new to java. What I can't seem to figure out is how NOT to implement overriding methods in a class from an interface because they are references. I don't want to make a new copy, and I can't just make (insert Class here) extend (the class the interface gets some of its methods from). So I implement it and what do i get?
err: The type Threadmanager must implement the inherited abstract method (the method)
and then it has a list, one of which says "implement uninherited methods".
But I dont want to implement any methods! I want to use them!
Threadmanager tm;
AwtUtils manager = tm;
manager.drawImage(/*params*/)
The above is what i want, the following is what i don't want:
#override
public void drawImage(/*params*/){
...
}
I don't want to redefine the methods in the interface, simply just use them. and I cant have class ThreadManager extends Debugger(.java) because it already extends something. I thought interfaces were a way you could use those methods in another class without inheriting them through "class foo extends bar"
By the way, all the methods referenced in the interface are references to methods in my class Debugger.java which doubles up as a debugger and the game library.
You cannot use methods from an interface. An interface has no code, only definitions. Think of it as a functionality contract that classes implementing it have to fulfill.
For example
public interface Example {
public void method1ToImplement();
public int method2ToImplement(final String input);
}
This is a contract that all classes implementing this interface must fulfill. This means any instantiable class that implements Example has to implement public void method1ToImplement() and public int method2ToImplement(String). This is because you're stating this class fulfills this functionality, so you must implement this funcionality because as of now there's no code for this functionality in your class since the interface contains no code. For example, you cannot use the methods in List, in fact you cannot even create a new List because it's an interface. But you can create and ArrayList and use its methods because it's a non-abstract class implementing the List interface.
Maybe you're confused because you saw somewhere else you can use already implemented methods, for example toString() (which is already implemented in all classes). This is because this method is not defined in an interface but by a parent class (in case of toString() it's Object that implements it).
TL;DR: A class implementing an interface must implement its methods unless it's abstract.
If I'm understanding you right, you want a class to implement an interface, but don't implement its methods. If that's so, you cannot. Implementation of interface methods is mandatory, unless you're writing an abstract class.
I'm guessing there's something missing on your question, so please, provide some code of your Interface and Class so that we could give you a better answer.
I think you're confused about what an interface does. An interface simply defines a contract such that any object which implements the interface must define the methods in the interface. If you have an abstract class, then you must implement the abstract methods of said class for any class that extends the abstract class. The only exception to this is when you extend from a class that has already implemented the abstract methods or interface and you don't want/need to redefine them for subclasses.
You say that you don't want to implement the methods, you just want to use them, but you can't use methods that don't exist. Implementing an interface does not magically define the logic in the methods in the interface--that is your job. Again, it simply states that any objects that implement the interface will have the interfaces' methods defined.
One of the nice things about interfaces is the following: Let's assume that we have a collection of objects that all implement a particular interface, then we can call any method from the interface on all those objects. NB: we can group said objects together by having an array, ArrayList, or what have you that take the interface as the type parameter, ie ArrayList<MyInterface>
More specific example:
Let's consider a Shape interface that solely includes the header for an area method. We can have a bunch of difference types of shapes that implement the Shape interface (circles, squares, etc). In each shape class, we define a method to get the area for said shape. Now, if we have an ArrayList<Shape> shapes =... we can put different types of shapes into that list and do the following:
for (Shape s : shapes)
{
System.out.println(s.area());
}

Where do I find tech spec overload abstract method in Java constructor?

I try Apache Wicket. This framework override abstract methods in java constructor.
Example:
public abstract class BasePage extends WebPage {
...
this.add(new Link<Void>("logout") {
#Override
public void onClick() {
...
}
});
}
I know it is possible to declare Class A whith abstract method my_method and declare Class B extends Class A and define real code for abstract method my_method in class B.
But I don't know any mechanism in Java to override an abstract method in constructor dynamically like in wicket.
How call these Java technology and where I can find a detailed technical specification of it?
It's called anonymous class, more here : http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
It's an anonymous inner class, basically an on-the-fly class for a special purpose that does not merit its own name or file. In some cases, you can see it quite often. This answer might shed more light on the topic. It also contains a link to the oracle specs.
You can find it here, on JLS specifications.

Must implement the inherited abstract method

My class implements ActionListener. I have implemented the following nested classes below:
JMenuItem mntmNew = new JMenuItem("New...");
mntmNew.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
doNew(e); //calls to outer class for cleaner code
}
});
mnFile.add(mntmNew);
JMenuItem mntmLoad = new JMenuItem("Load...");
mntmLoad.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
doLoad(e); //calls to outer class for cleaner code
}
});
mnFile.add(mntmLoad);
//etc. for the rest of the menu system
However, Eclipse is still telling me that my class must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent e). Can you not implement override methods in a nested class in this way?
Your question:
Can you not implement override methods in a nested class in this way?
The answer is no. Eclipse (actually Java) is complaining that while you're declaring your class as implementing ActionListener you're not giving your class the necessary actionPerformed(...) method in the class's own scope -- and this last part is very important. The class that implements the interface must implement all the interface's required methods in its own scope and not in nested classes. Note that this doesn't prevent you from nesting classes that also implement ActionListener or other interfaces, but regardless, the rule remains that a non-abstract class that implements an interface must override all of the interface's methods.
But since you're not using objects of your class as an ActionListener, the simple solution is to not declare your class as implementing the ActionListener interface. Problem solved. And actually you're far better off not having your GUI class implement your listener interfaces since combining them in one class is asking a class to do too much. In technical terms, it unnecessarily reduces a class's cohesion and risks increasing it's coupling reducing its readability and maintainability.

Type must implement the inherited abstract method TextToSpeech.OnInitListener.onInit(int)

Trying to become a bit more experienced, I follow the advice: code, code, then code some more. I am, however, completely new to OOP, Java and Android.
I found this on-line coding example: Build a speak and repeat app.
The code:
// extend the opening class declaration and implement the OnInitListener interface for TTS functionality
public class RepeatActivity extends Activity implements OnClickListener, OnInitListener
... produces this error in Eclipse Indigo:
The type RepeatActivity must implement the inherited abstract method TextToSpeech.OnInitListener.onInit(int)
Does this have something to do with:
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
I have no idea how to solve this. Please advise. It is difficult to ask concrete questions about a subject one knows practically nothing about.
Cheers,
Pieter
Normally, when your class implements your interface, it has to redefine all the methods in the interface. This is why it is called an interface:)
Implement all the methods in your interfaces which are declared with your class..onInit() method may be there in OnInitListener. you must implement it in your class.
Add a method in your RepeatActivity as follows.
public void onInit (int status){
//what you want to do just after the completion of the TextToSpeech engine initialization
}
You are saying that your class implements an interface, but your class doesn't contain all of the methods that said interface includes.
In simple terms, an interface is a contract that guarantees a class can perform specific functions, without saying how they go about doing them.

Categories

Resources