IntelliJ IDEA 14.1.0 Help

Use Interface Where Possible

Use Interface Where Possible refactoring delegates execution of the specified methods, derived from a base class/interface, to an instance of an ancestor class or an inner class, implementing the same interface.

Example

Before After
// File Class.java ublic class Class implements Interface { publicMethod() { hiddenMethod() {
// File Class.java UNCHANGED ublic class Class implements Interface { publicMethod() { hiddenMethod() {
// File Interface.java ublic interface Interface { ONSTANT=0; ethod();
// File Interface.java UNCHANGED ublic interface Interface { ONSTANT=0; ethod();
// File AnotherClass.java ublic class AnotherClass { lass; Class; method() { ss.publicMethod(); ss.hiddenMethod(); ass.publicMethod(); ndClass nstanceof Class) {
// File AnotherClass.java MODIFIED ublic class AnotherClass { lass; condInterface; method() { ss.publicMethod(); ss.hiddenMethod(); terface.publicMethod(); ndInterface nstanceof Interface) {

To use interface where possible

  1. Select a class whose methods should be delegated to its parent class or interface. To do that, place the caret on this class in the editor or in one of the views (Project, Commander).
  2. On the main menu or on the context menu of the selection, choose Refactor | Use Interface Where Possible.
  3. In the Use Interface Where Possible dialog box, select the parent object, which will replace the usages of the current class.
  4. To replace the current class name in instanceof statements, check the option Use interface/superclass in instanceof.
  5. To review the intended changes and make final corrections prior to the refactoring, click Preview. To continue without preview, click Refactor.
  6. The Rename variables dialog appears. It lists the occurrences of the class, that may be replaced by the superclass or the interface selected. Select the usages you want to replace, and (optionally) specify new names for each of them.
  7. Click OK to continue. If you have previously clicked the Preview button, the Preview window appears now.

See Also

Last modified: 24 March 2015
comments powered by Disqus