java 8 - Repeated overriding of functional interface's abstract method? -



java 8 - Repeated overriding of functional interface's abstract method? -

i have functional interface in java 8:

public interface ifunclambda1 { public int someint(); }

in main:

ifunclambda1 ifuncl1 = () -> 5; system.out.println("\nifuncl1.someint: " + ifuncl1.someint()); ifuncl1 = () -> 1; system.out.println("ifuncl1.someint: " + ifuncl1.someint());

running yield:

ifuncl1.someint: 5 ifuncl1.someint: 1

is functionality ok is? intended?

if overriding done in implementing class, , implementation alter @ point, in every place that method called, behaviour same, have consistency. if alter behaviour/implementation through lambda expressions in example, behaviour valid til next change, later on in flow. feels unreliable , hard follow.

edit: @assylias don't see how someint() has behaviour changed... if added param someint , have code:

ifunclambda1 ifuncl1 = (x) -> x - 1; system.out.println("\nifuncl1.someint: " + ifuncl1.someint(var)); ifuncl1 = (x) -> x + 1; system.out.println("ifuncl1.someint: " + ifuncl1.someint(var));

with var beingness final even, how re-write classes?

in example, () -> 5 1 object , () -> 1 object. happen utilize same variable refer them how references work in java.

by way behaves same way if had used anonymous classes:

ifunclambda1 ifuncl1 = new ifunclambda1() { public int someint() { homecoming 5; } }; system.out.println("\nifuncl1.someint: " + ifuncl1.someint()); ifuncl1 = new ifunclambda1() { public int someint() { homecoming 1; } }; system.out.println("ifuncl1.someint: " + ifuncl1.someint());

or using "normal" classes:

public static class implements ifunclambda1 { private final int i; public a(int i) { this.i = i; } public int someint() { homecoming i; } } ifunclambda1 ifuncl1 = new a(5); system.out.println("\nifuncl1.someint: " + ifuncl1.someint()); ifuncl1 = new a(1); system.out.println("ifuncl1.someint: " + ifuncl1.someint());

there 1 time again there 2 instances of a lose reference first instance when reassign ifuncl1.

java-8 functional-interface

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -