Skip to content

Commit 089f30d

Browse files
committed
Added example of Method Reference for a super method of a particular object and fixed for instance method of an arbitrary object of a particular type.
1 parent fb4f9b3 commit 089f30d

3 files changed

Lines changed: 69 additions & 2 deletions

File tree

core-java-8/src/main/java/com/baeldung/doublecolon/Computer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ public void setHealty(final Integer healty) {
4444
this.healty = healty;
4545
}
4646

47+
public void turnOnPc() {
48+
System.out.println("Computer turned on");
49+
}
50+
51+
public void turnOffPc() {
52+
System.out.println("Computer turned off");
53+
}
54+
55+
public Double calculateValue(Double initialValue) {
56+
return initialValue/1.50;
57+
}
58+
4759
@Override
4860
public String toString() {
4961
return "Computer{" + "age=" + age + ", color='" + color + '\'' + ", healty=" + healty + '}';
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.doublecolon;
2+
3+
import java.util.function.Function;
4+
5+
6+
public class MacbookPro extends Computer{
7+
8+
public MacbookPro(int age, String color) {
9+
super(age, color);
10+
}
11+
12+
public MacbookPro(Integer age, String color, Integer healty) {
13+
super(age, color, healty);
14+
}
15+
16+
@Override
17+
public void turnOnPc() {
18+
System.out.println("MacbookPro turned on");
19+
}
20+
21+
@Override
22+
public void turnOffPc() {
23+
System.out.println("MacbookPro turned off");
24+
}
25+
26+
@Override
27+
public Double calculateValue(Double initialValue){
28+
29+
Function<Double,Double> function = super::calculateValue;
30+
final Double pcValue = function.apply(initialValue);
31+
System.out.println("First value is:" +pcValue);
32+
return pcValue + (initialValue/10) ;
33+
34+
}
35+
}

core-java-8/src/test/java/com/baeldung/doublecolon/TestComputerUtils.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void tearDown() {
2222
}
2323

2424
@Test
25-
public void testFilter() {
25+
public void testConstructorReference() {
2626

2727
Computer c1 = new Computer(2015, "white");
2828
Computer c2 = new Computer(2009, "black");
@@ -53,7 +53,7 @@ public void testFilter() {
5353
}
5454

5555
@Test
56-
public void testRepair() {
56+
public void testStaticMethodReference() {
5757

5858
Computer c1 = new Computer(2015, "white", 35);
5959
Computer c2 = new Computer(2009, "black", 65);
@@ -66,4 +66,24 @@ public void testRepair() {
6666
Assert.assertEquals("Computer repaired", new Integer(100), c1.getHealty());
6767
}
6868

69+
@Test
70+
public void testInstanceMethodArbitraryObjectParticularType() {
71+
72+
Computer c1 = new Computer(2015, "white", 35);
73+
Computer c2 = new MacbookPro(2009, "black", 65);
74+
List<Computer> inventory = Arrays.asList(c1, c2);
75+
inventory.forEach(Computer::turnOnPc);
76+
77+
}
78+
79+
@Test
80+
public void testSuperMethodReference() {
81+
82+
final TriFunction<Integer, String, Integer, MacbookPro> integerStringIntegerObjectTriFunction = MacbookPro::new;
83+
final MacbookPro macbookPro = integerStringIntegerObjectTriFunction.apply(2010, "black",100);
84+
Double initialValue=new Double(999.99);
85+
final Double actualValue = macbookPro.calculateValue(initialValue);
86+
Assert.assertEquals(766.659, actualValue,0.0);
87+
}
88+
6989
}

0 commit comments

Comments
 (0)