0

I have to resolve this task for my college... I need to create one new class (with one new method, just to show I know how to do it) which extends swt class which is final (so it can't be extended in a classic way with extend keyword..). I have been told I can write wrapper class and instantiate final class object inside it. But what then?

I would be really thankful if someone can show me some example of this kind of extending (with some final class, new class and using method of new class which extends final class), because I have no idea how to do it.

4
  • Does this class have an interface? Commented Feb 1, 2014 at 14:43
  • Mureinik - Class doesn't have an interface... Greg - GridLayout class Commented Feb 1, 2014 at 15:01
  • For GridLayout this sounds like it is probably the wrong way to do whatever it is you want. Commented Feb 1, 2014 at 15:20
  • The task you're given should not be a task at all. You DON'T need to know how to extend a final class - and thus mess with the given framework. If the class is final and shouldn't be, due to new requirements or bad architecture, then it should be fixed, instead of trying to override it. This task is ridiculous! Commented Feb 1, 2014 at 15:22

1 Answer 1

1

Use the decorator pattern to wrap the object and extend it.

To add more to this answer:

The GridLayout extends Layout. So create a wrapper around the GridLayout extending Layout and add your extension to the wrapper. If you want to change the way, the layout is performed you add your code to the layout method.

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

5 Comments

While the link might be correct, you should expand on the answer.
Thanks :) I think this might be useful, I'll need to read it carefully :)
This is a similar question, which also has the decorator pattern as its solution: stackoverflow.com/questions/21200421/…
@aliteralmind I guess that is why they call it a pattern ;)
Actually, it's adapter, but it feels related.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.