Eclipse add abstract method in abstract class - eclipse

In Eclipse, after I added one abstract method in my abstract class, is there a way to ask Eclipse add an empty implementation in all subclasses of that abstract class?

You can select the "type Foo must implement inherited abstract bar()" in the problems view, select Quick Fix from the context menu and apply the fix to all affected classes. However, they will only get the empty method stub so I'm not aware of a way to add the same implementation body to all subtypes.

Related

Choose between abstract class and interface

Among my two processes' functionality, there is a common function to merge files. I need not going to insist any of the processes to have some methods as interface does. And, also the two processes are independent. So, is it fine I just go with an Abstract class and have the implementation in that abstract class itself? Also I do not need any abstract method.
Inheritance is used when there is IS-A relation between subclass and the base class. I don't think it is the case here. You didn't specify the language, but from your profile I guess you use Java. So if you use an Abstract Class you won't be able to inherit from other, more appropriate class in the future.
Instead of inheritance you can use composition. Which means that you create a regular file merging class which has this method to merge files. And in classes where you want to have this functionality you just instantiate this new file merging class. It lets you inherit from other class in the future.
If you want to inform the world that those classes can merge files (to use polymorphism), and you use Java 8 you can create default method inside an interface and implement this interface without override this default method. But I think composition will be better in this case.

Eclipse RCP - Custom FilteredPreferenceDialog

I'm trying to implement a custom preference dialog using the FilteredPreferenceDialog class.
The problem that this is an abstract class, but I dont really understand why. It has no abstract methods. I created my own class which extends FilteredPreferenceDialog but then I get the discouraged access warnings. There is another class called WorkbenchPreferenceDialog which also extends FilteredPreferenceDialog, and its also abstract.
Is there a class which is a public not abstract class I can create which has the filtering implementation? The PreferenceDialog class works fine except it doesnt have the filtering mechanism.

FXCop rule Interface methods should be callable by child types

When running FxCop I get the error that interface methods should be callable by child types.
The resolution states the following:
"Make 'MenuPreview' sealed (a breaking change if this class has previously shipped),
implement the method non-explicitly, or implement a new method that exposes
the functionality of 'IComponentConnector.Connect(int, object)'
and is visible to derived classes."
I get this for all classes the derive from Window or some other UI class. Is this a red herring that I can ignore, or is there something I should be doing?
I think the issue is that if an interface is implemented explicitly, it will be impossible for a derived class to both change the interface behavior and make use of the base-class behavior. A common pattern to get around this difficulty in cases where explicit interface implementation would be required is to have the interface do nothing but call a protected virtual method, and have any derived classes that wish to override the behavior of the interface do so by means of the protected virtual method.
Consider IDisposable.Dispose(). If the code in an explicit implementation were actually responsible for performing the disposal, there would be no way for a derived class to add its own dispose logic except by reimplementing IDisposable, and there would be no way for a class which reimplemented IDisposable to access its parent's Dispose method. Although Microsoft could have had IDisposable.Dispose call a protected function with a different name, it opted to use the same name but add a dummy parameter of type Boolean.

Refactoring in eclipse: how to add field,getter,setter to all implementations of an interface?

how can I add a field, getter and setter to all implementations of MyInterface (in the current project or folder?)
Open the source file containing the
interface. Select the interface's
name and hit F4 (Open type
hierarchy).
In the type hierarchy view, select all of the classes that implement your interface. The hierarchy is displayed in a tree-like fashion so selection should be very easy.
Right-click over your selection, select Source, then Override/Implement Methods....
Mission accomplished.
[Edited]
When I wrote these steps, I thought that your intention was to create stubs of newly-introduced interface methods, in all classes that implement that interface.
Now that I am re-reading your request, I'm having a hard time understanding what it is exactly that you want to do. You wrote:
how can I add a field, getter and setter to all implementations of MyInterface
So, you have an interface named MyInterface and 1,000 classes implementing it.
You would like to introduce a new field, a getter and a setter for that field. So I guess my (and perhaps others') first difficulty is that you can't add a field to an interface, unless it's final - so your wish to "add a field to an interface" just doesn't sound right.
I suppose it would help if you give us a 30,000ft high-level diagram / explanation about your hierarchy and exactly what it is that you're trying to accomplish.
I don't think there's a one-step way to go about this. You could add the getter & setter to the interface, then let the compiler tell you where your implementers are, and it should be a simple copy & paste to place the field, getter, and setter into every class after the first.
Alternatively, you could transform your interface into an abstract class, or introduce an abstract class between your interface and your concrete classes, but that's only if your classes don't already extend other classes.

When to use an abstract class with no interface?

Whenever I create an abstract class I tend to create an interface to go along with it and have other code refer to the interface and not the abstract class. Usually when I don't create an interface to start with I regret it (such as having to override all implimented methods to stub the class for unit testing or later down the line new classes don't need any of the implimentation and override everything also finding themselves unable to extend any other class).
At first I tried to distinguish when to use an interface and when to use an abstract class by considering is-a vs able-to but I still would end up suffering later down the line for not making an interface to start with.
So the question is when is it a good idea to only have an abstract class and no interface at all?
When you wish to "give" some base class functionality to derived classes but when this functionality is not sufficient to instantiate a usable class, then go for abstract classes.
When you wish that some classes completely implement a set of methods (a public contract), then it is a convenient to define such contract with interfaces and enforce them onto classes by making them inherit this interface.
In short:
With abstract classes you give some common base functionality to derived classes. No further actions are necessary unless abstract class has some stubs (which have to be implemented down there).
With interfaces you require derived classes to implement a set of functions and you do not pass along any implementation.
So the question is when is it a good idea to only have an abstract class and no interface at all?
When you do not wish to enforce any public contract (a set of methods/properties defined by an interface).
Also when you do not plan to use certain coding techniques like casting object to an interface type (run-time polymorphism) or limit allowed input (some method argument will only accept object of types which implement certain interfaces).
Well, the main case it is useful to have only an abstract class without any interface is to mark a certain type. It is useful to be able to check if an object "is-a" something. These interface "mark" an objet to be of a certain type. Depending on the language you use, different design patterns apply ...
These sort of abstract classes exist in java. You can also use them in C++ with RTTI.
my2c