0

I have a general question: Is it possible to create a class, which is an original copy of another class, but contains additional variables?

For example I know that this is possible with a JPanel:

public class CustomPanel extends JPanel{
    int exampleVariable = 0;
}

This is possible without any problems. But can I do this with other classes too? In my case I want to do this with Image - but that doesn't seem to be possible, when I add extends Image I have to implement required methods, and that means I have to program a new Image class. Is it possible in another way?

Edit: My aim was to add a method to set the images name...

4 Answers 4

5

java.awt.Image is an abstract class.

Therefore you must override all its methods when extending it.

If you extend a non-abstract class, the methods are implemented, therefore you may choose to override them (as long as they're not final - or the class itself is not final).

However, an abstract class is designed specifically to be extended by implementing its methods through @Override.

Here is a good starting point to investigate further.

Sign up to request clarification or add additional context in comments.

3 Comments

This is what he does not want to.
Mena is explaining why the OP is getting the behaviour they observed.
@selig thanks. Will elaborate on my answer though just for clarity.
2

Image is an abstract class, you want to extend one of it's subclasses, either BufferedImage or VolatileImage.

Comments

1

In order to do what you want, you would have to have access to the concrete class that extends Image that you are really using. If you had that access, you could then create a class extending it with the added variables.

Consider, for example, java.util.AbstractList and java.util.ArrayList. You could extend ArrayList, and only implement what you wanted to add. You would have to implement more to extend AbstractList.

In some cases, the implementing class is private, and may change from run to run depending on the situation, so it is not always possible.

If you cannot identify the correct concrete Image subclass to extend, I suggest considering composition. Create a class that has an Image reference, a method getImmage that returns it, and also the data you want to associate with the Image. Pass around and store a reference to an instance of your class, but whenever you need to do Image operations use the getImage method to get the Image reference.

Comments

1

Image is an abstract class. So , if you extend it you would have to implement all of its abstract methods which is like reinventing the whole wheel. However, You can extend the class which already implements the Image class and has all the methods already implemented in it. For example: BufferedImage . It will be something like:

public class CustomImage extends BufferedImage{
    // Override its non final methods of your concern..
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.