Showing posts with label Interface. Show all posts
Showing posts with label Interface. Show all posts

Thursday, May 19, 2011

Published 5/19/2011 by with 5 comments

is not abstract and does not override abstract method

See the code below:


import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame implements ActionListener{
private JButton exitButton = new JButton("Exit");
public MyFrame()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(exitButton);
  exitButton.addActionListener(this);
setSize(300,200);
}
}

If this code is compiled, we will see the error "MyFrame is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener"

Why? When we implement an interface(ActionListener in this example) or extend an abstract class in a concrete class, we must override the abstract method(s). In the above example we have implemented ActionListener but have not overridden the abstract method actionPerformed.

The correct code of the above is:

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MyFrame extends JFrame implements ActionListener
{
private JButton exitButton = new JButton("Exit");
public MyFrame()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
container.add(exitButton);
exitButton.addActionListener(this);
setSize(300,200);
}
public void actionPerformed(ActionEvent ae)
{
System.exit(0);
}
}


Sometimes it happens that we override the abstract method with wrong name spelling or wrong signature, then also we see the same error message. Note that Java is a case sensitive language; most often we write the method name with wrong case (example, actionperformed/ActionPerformed instead of actionPerformed) and see this error message.

For implementing an interface or extending an abstract class, we must override all of the abstract methods (if more than one), though we do not need all. For example, if we implement KeyListener we must override each of the three abstract methods keyPressed, keyTyped and keyReleased though we may not need all of them. In that case, method body can be kept blank. Instead of interface, we can use adapter class (if available) to get rid of overriding all abstract methods.

In summary, override the abstract method with correct name spelling and signature to remove the error.
Read More
    email this       edit

Monday, May 9, 2011

Published 5/09/2011 by with 1 comment

modifier static not allowed here


Java error "modifier static not allowed here" can occur for any of the following reasons:

1. Constructor is declared as static(!)

A constructor cannot be static. In the wrong code below:

public class Person
{
private String firstName;
private String lastName;
public static Person()
{
//...
}
}

Here the constructor Person is declared as static. As the constructor must be non-static, the correct code is:

public class Person
{
private String firstName;
private String lastName;
public Person()
{
//...
}
}



2. Interface contains static method(!)

All methods of any interface is abstract and hence they must be non static. See the code below:

public interface MyInterface
{
public static void myMetod();
}

The above code is wrong as the interface method is declared as static. Corrected code is:

public interface MyInterface
{
public void myMetod();
}
Read More
    email this       edit
Published 5/09/2011 by with 1 comment

interface methods cannot have body

Java error "interface methods cannot have body" occurs when body of the method within an interface is defined.

See the code below:

public interface MyInterface
{
public void myMetod()
{
}
}

Here the method myMethod has body starting at the symbol { and ending at }. An interface can have abstract methods (method without any body). The class that implements the interface defines the body of the interface methods.

So to remove the error, the method signature/header will end by ; (semi colon) and there will be no body section for the method. Below is the correct code:



public interface MyInterface
{
public void myMetod();
}


Read More
    email this       edit
Published 5/09/2011 by with 0 comment

interface expected here

The code below leads to an error "interface expected here"

public interface MyInterface extends Thread
{
}

Here an interface is created (MyInterface) and it inherits(extends) the Thread class which is not possible at all. Why? Because an interface can inherit only another interface, not any class. As Thread is a class, it cannot be inherited by any interface.

The code below is correct as the interface inherits another interface Runnable



public interface MyInterface extends Runnable
{
}


A similar type of error is posted at  no interface expected here

Read More
    email this       edit
Published 5/09/2011 by with 0 comment

no interface expected here

Any Java code as below will lead an error "no interface expected here"

import java.io.Serializable;
public class Person extends Serializable
{
private String firstName;
private String lastName;
//...
}

See that, here Person is a class (public class Person) and it inherits an interface (extends Serializable). Note that a class can inherit another class only, not any interface. A class can implement one or more interface only. Serializable is an interface, not a class.

Actually the correct code of the above will be:


import java.io.Serializable;
public class Person implements Serializable
{
private String firstName;
private String lastName;
//...
}

Here it is notable that, only an interface can inherit another interface.

A similar type of error is posted at interface expected here
Read More
    email this       edit

Wednesday, August 19, 2009

Published 8/19/2009 by with 5 comments

class, interface, or enum expected

You see this error message because of any one of the followings:

1. You misspelled the keyword class (fully lower case) or you did not write it at all for writing your class.

public Class Test //actual keyword is class
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

In the above example Class (C is in upper case) is written instead of class (fully lower case).

2. Remember you cannot declare variables outside of class body like below.

int variable; //variable must be declared inside the class body
public class Test
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}

To solve this declare variables inside the class body as below.


public class Test
{
int variable; //variable must be declared inside the class body
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}


3. All methods are also defined within the method body. If you define any method outside of the class body you will see this error message.


public class Test
{
public void myMethod()
{
System.out.println("method inside the class body");
}
} //end of class

public void anotherMethod()
{
System.out.println("method outside of the class body");
}

In the above code, the method anotherMethod is defined after the end of the class. So this error occurred. Most often it occurs just because of incorrect use of {} (curly braces). Check all { are closed with } in the proper place.

4. If you are defining an interface, check you spelled it correctly in fully lowercase.

public Interface Test //actual keyword is interface
{
public void myMethod();

}

In the above code, keyword interface is misspelled with upper I.

5. Same way if you define an enum spell the keyword enum correctly.

public Enum Day //actual keyword is enum
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}

In the above example Enum is incorrect as the E is in upper case, it will be lower case e.
Read More
    email this       edit