im having a problem getting an abstract method to work, heres the code

abstract ImageIcon getPicture();
	{
		return picture;
	}

When i compile the code i get the following error:

Appliance.java:30: return outside method
return picture;

Can anyone help me fix this?

Recommended Answers

All 5 Replies

You can't instantiate an abstract class or call an abstract method as far as I know. What exactly are you trying to do? Post the code where you're trying to "get the abstract method to work".

Heres the appliance.java code

import javax.swing.*;

abstract class Appliance
{
	boolean power;
	ImageIcon picture;
	
	Appliance(boolean po, ImageIcon pi)
	{
		power=po;
		picture=pi;
	}
	
	boolean getPower()
	{
		return power;
	}
	
	void setPower(boolean po)
	{
		power = po;
	}
	
	abstract ImageIcon getPicture();
	{
		return picture;
	}
	
	void showPower()
	{
		if(power = true)
		{
			System.out.println("Power: ON");
		}
		if(power = false)
		{
			System.out.println("Power: OFF");
		}
		else
		{
			System.out.println("ERROR");
		}
	}
}

This class should have a getter method for the picture variable which is declared abstract
This class is to be extended by another class called lamp

Read
http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

Particularly, where it defines what an abstract method is.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon). . .

Then compare that definition to how you have coded your method. You'll notice that you cannot declare a method "abstract" while also defining the method body. So either get rid of the "abstract" and the semicolon, or get rid of the brackets and the return picture.

Invalid:

abstract ImageIcon getPicture();
{
	return picture;
}

Valid:

abstract ImageIcon getPicture();

Valid:

ImageIcon getPicture()
{
	return picture;
}
commented: Helped me fix my abstract method error +1

Thanks, that solved the error and appliance.java will now compile.

Yeah, because when you extend a class, there is an implicit call to super(). What super() does is it calls the default constructor (the one with no arguments) from the super class. You're getting an error because your class Appliance has no default constructor, so either add a default constructor, or use (this is an example) super(true, new ImageIcon());

(the reason for this is because you have to create everything that your superclass, Appliance, has in it. So the compiler automatically calls super(), which calls your default constructor, to do this. But since you don't have a default constructor, you either have to write one, or call your other constructor, which is super(true, new ImageIcon());

Basically put the call to super(parameters for one of your Appliance class's constructors go here) as the first statement in all of the constructors for any class that extends Appliance. Either that or make a default constructor for the Appliance class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.