Can lambda expressions be the alternative of polymorphism?

  • A+
Category:Languages

I am learning lambda expressions and functional interfaces. We can directly write an implementation of the interface by the lambda expression. So I think, it could be the alternative for polymorphism.

I have some code using polymorphism,

interface Drawable {     public void draw(); }   class Shape {      protected String name;      public Shape(String name) {         this.name = name;     } }  class Rectangle extends Shape implements Drawable  {      public Rectangle(String name) {         super(name);     }      @Override     public void draw() {         System.out.println("I am "+this.name);         System.out.println("Drawing rectangle with 2 equal sides.");     } }  class Square extends Shape implements Drawable {      public Square(String name) {         super(name);     }      @Override     public void draw() {         System.out.println("I am "+this.name);         System.out.println("Drawing square with 4 equal sides.");     } }   public class DrawShape {      public static void main(String ar[]) {          Drawable rectangle = new Rectangle("Rectangle");         rectangle.draw();          Drawable square = new Square("Square");         square.draw();      } } 

I have written above code using lambda expressions and functional interface,

@FunctionalInterface interface Drawable {     public void draw(); }   class Shape {     private String name;     public Shape(String name) {         this.name = name;     }      public void draw(Drawable d1) {         System.out.println("I am "+this.name);         d1.draw();     } }    public class DrawShape {      public static void main(String[] args) {         Shape s1 = new Shape("Rectangle");         Drawable rectangle = () -> System.out.println("Drawing rectangle with 2 equal sides.");         s1.draw(rectangle);          Shape s2 = new Shape("Square");         Drawable sqaure = () -> System.out.println("Drawing square with 4 equal sides.");         s2.draw(sqaure);     }  } 

Which is the better approach? What about other aspects like code reusability, code maintenance and modification, coupling and cohesion etc for lambda?

 


I would argue that lambda expressions allow developers to write fully polymorphic types, the way full class implementations do.

Polymorphism is often seen in two ways:

Drawable drawable = new Rectangle("name"); drawable.draw(); Shape shape = (Shape) drawable; //same object, multiple types. 

And:

Drawable drawable2 = new Rectangle("name"); drawable2.draw(); //Rectangle.draw() implementation invoked drawable2 = new Square("name"); drawable2.draw(); //Square.draw() implementation 

Neither of these two is perfectly allowed for by lambda expressions:

  1. Lambda expressions will only be used to implement functional interfaces. This is the first major limitation.
  2. Although one can do this:

    Drawable drawable = () -> System.out.println("drawing rectangle"); drawable = () -> System.out.println("drawing square"); 

    This is not strictly the same thing as the second code snippet above. The class version uses dynamic binding, whereas the lambda expressions depend on the compiler. Also, one would be correct to argue that the two assignments above have 2 different objects from 2 different sources.

  3. One can't just "cast" types as with classes:

    Drawable drawable3 = () -> System.out.println("Drawing something"); Shape shape3 = (Shape) drawable3; //Class cast exception. 

In other words, lambda expressions are a good fit for functional programming coding, not a substitute for good Object-Oriented design.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: