Does check for interface implementation inside an abstract class is an anti-pattern?

Asked
Active3 hr before
Viewed126 times

8 Answers

interfacecheck
90%

is the above pattern an anti-pattern ?,Thanks for contributing an answer to Stack Overflow!, Stack Overflow Public questions & answers , @Thilo isn't factory method about having a separate factory class rather than having the getInstance() method inside the base class – Dev Blanked May 4 '13 at 6:43

No it's not. It's more like factory method pattern http://en.wikipedia.org/wiki/Factory_method_pattern. E.g. Calendar.getInstance();. JDK is full of such examples. Also reminds of Effective Java Item 1: Consider static factory methods instead of constructors

factory method pattern

No it's not. It's more like factory method pattern http://en.wikipedia.org/wiki/Factory_method_pattern. E.g. Calendar.getInstance();. JDK is full of such examples. Also reminds of Effective Java Item 1: Consider static factory methods instead of constructors

Calendar.getInstance();

No it's not. It's more like factory method pattern http://en.wikipedia.org/wiki/Factory_method_pattern. E.g. Calendar.getInstance();. JDK is full of such examples. Also reminds of Effective Java Item 1: Consider static factory methods instead of constructors

Item 1: Consider static factory methods instead of constructors
load more v
88%

Now as all methods in an interface are abstract methods therefore we can implement it using Abstract Class.,Here the three  non-implemented methods are the abstract methods,2. Now let’s implement the interface in an Abstract class named Student:,Here we have overridden two abstract methods of the interface  GFG.

load more v
72%

The interface is a blueprint that can be used to implement a class. The interface does not contain any concrete methods (methods that have code). All the methods of an interface are abstract methods.,Abstract classes allow you to create blueprints for concrete classes. But the inheriting class should implement the abstract method.,Following is sample code to create an interface and abstract class in Java,An interface cannot be instantiated. However, classes that implement interfaces can be instantiated. Interfaces never contain instance variables but, they can contain public static final variables (i.e., constant class variables)

Interface Syntax

interface name {
   //methods
}
load more v
65%

Correct abstractions are focused and strive to have the minimal necessary interface.,The false abstraction is any interface, base class, or adaptor that should not exist. While the author may have been well meaning, they probably violated the YAGNI principle, and now we’re left with harder to maintain code. Let’s look at how to identify this antipattern.,Thanks again for putting a name to this. Hopefully it advances the cause of folks understanding what a meaningful abstraction is.,An abstract type with a lot of methods or properties may be a false abstraction. As more detail is added it becomes more concrete, and less flexible.

Single implementation

An abstract type that has only a single implementation may be a false abstraction. These often following a naming convention of Fluff and FluffImpl. The suffix Impl, or the full Implementation is generally an indicator of a false abstraction.

Fluff

Single implementation

An abstract type that has only a single implementation may be a false abstraction. These often following a naming convention of Fluff and FluffImpl. The suffix Impl, or the full Implementation is generally an indicator of a false abstraction.

FluffImpl

Single implementation

An abstract type that has only a single implementation may be a false abstraction. These often following a naming convention of Fluff and FluffImpl. The suffix Impl, or the full Implementation is generally an indicator of a false abstraction.

Impl

Single implementation

An abstract type that has only a single implementation may be a false abstraction. These often following a naming convention of Fluff and FluffImpl. The suffix Impl, or the full Implementation is generally an indicator of a false abstraction.

Implementation
load more v
75%

Note also that this approach also fits nicely with the composition over inheritance principal.,This process repeats until BaseController has ten subclasses and 75 shared methods. ,If it’s the latter, callers will invoke methods implemented in your subclasses, which in turn will call methods in the abstract class. There’s a good chance that a swiss-army anti-patten is forming.,Swiss army controllers generally come into existence like this: 

Let’s fabricate a quick example:

public abstract class Base {

   public void doSomething() {
      System.out.println("Doing something...")
   }

   public abstract void doSomethingElse();

}
load more v
40%

There is no need to use abstract class in JDK 8. We can implement the default method in interface itself.,Is this compulsory to use abstract method in abstract class or we can use abstract method any where,Yes, a lot of abstraction makes code complex due to lot of indirection. A right level of Abstraction make code flexible and readable at same time. ,Not completely true...you may want state included as part of your superclass in which case you would still use an abstract class.


import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Java Program to demonstrate what is abstract class and 
 * abstract method in Java, how to use
 * them, when to use them with a practical example.
 *
 * @author Javin Paul
 */
public class AbstractClassDemo {

   public static void main(String args[]) {
      Fruit mango = new Mango(Color.YELLOW, true); // mango is seasonal
      Fruit banana = new Banana(Color.YELLOW, false);
      // banana is not seasonal

      List & lt;
      Fruit & gt;
      platter = new ArrayList & lt;
      Fruit & gt;
      ();
      platter.add(mango);
      platter.add(banana);
      serve(platter);
   }

   public static void serve(Collection & lt; Fruit & gt; fruits) {
      System.out.println("Preparing fruits to serve");
      for (Fruit f: fruits) {
         f.prepare();
      }
   }
}

/*
 * Abstract class to represent Fruit, defined only essential
 * properties of Fruit here and make things abstract which
 * is different for different fruits.
 */
abstract class Fruit {
   private Color color;
   private boolean seasonal;

   public Fruit(Color color, boolean seasonal) {
      this.color = color;
      this.seasonal = seasonal;
   }

   /*
    * This is an abstract method, see it doesn't have method body, 
    * only declaration
    */
   public abstract void prepare();

   public Color getColor() {
      return color;
   }

   public boolean isSeasonal() {
      return seasonal;
   }
}

/*
 * A concrete class to extend Fruit, since Mango IS-A Fruit
 * extending Fruit is justified. it got all properties of
 * fruit, and it defines how to prepare mango for eating.
 */
class Mango extends Fruit {

   public Mango(Color color, boolean seasonal) {
      super(color, seasonal);
   }

   @Override
   public void prepare() {
      System.out.println("Cut the Mango");
   }
}

/*
 * Another concrete class to extend Fruit.
 */
class Banana extends Fruit {

   public Banana(Color color, boolean seasonal) {
      super(color, seasonal);
      // TODO Auto-generated constructor stub
   }

   @Override
   public void prepare() {
      System.out.println("Peal the Banana");
   }
}

Output:
   Preparing fruits to serve
Cut the Mango
Peal the Banana
22%

We will dive deeper into Object Oriented Programming and try to think in terms of Design Patterns to share and enforce our code using Polymorphism.,There is another Object Oriented Programming concept that is closely related to Abstract Classes called Interface.,In this article, you'll learn how to share and enforce code with polymorphism using abstract classes and interfaces., Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons - all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Let’s say we have a class called Man with some properties (name, age, height, fav_drinks and fav_sports) and methods (giveFirmHandshakes, beStubborn and notPutToiletPaper).

< ? php

class Man {
   public $name;
   public $age;
   public $height;
   public $fav_sports;
   public $fav_drinks;

   public
   function __construct($name, $age, $height) {
      $this - > name = $name;
      $this - > age = $age;
      $this - > height = $height;
   }

   public
   function giveFirmHandshakes() {
      return "I give firm handshakes.";
   }

   public
   function beStubborn() {
      return "I am stubborn.";
   }

   public
   function notPutToiletPaper() {
      return "It's not humanly possible to remember to put toilet paper rolls when they are finished";
   }
}
load more v
60%

11 Allowing multiple classes to implement Interfaces and having your code depend on the interfaces is ESSENTIAL to isolation for unit testing. If you are doing unit testing, you will have another class implementing that interface. – StuperUser Aug 7 '12 at 14:10 ,Isn't the whole point of an interface that multiple classes adhere to a set of rules and implementations?,Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.,Even though module B has only one implementation of its interface, the interface is still necessary.

You could have an interface that looked like this:

Interface ICabin
Event IgnitionSwitchTurnedOn()
Event IgnitionSwitchTurnedOff()
Event BrakePedalPositionChanged(int percent)
Event GasPedalPositionChanged(int percent)
Event GearShiftGearChanged(int gearNum)
Event SteeringWheelTurned(float degree)
End Interface
load more v

Other "interface-check" queries related to "Does check for interface implementation inside an abstract class is an anti-pattern?"