This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Food { | |
public abstract void giveEnergy () ; | |
} | |
public class Burger extends Food { | |
@Override | |
public void giveEnergy () { | |
System.out.println ("Burger gives high energy"); | |
} | |
} | |
public class Salad extends Food { | |
@Override | |
public void giveEnergy () { | |
System.out.println ("Salad does not give energy"); | |
} | |
} | |
package com.company.samples.way6; | |
import java.util.function.Consumer; | |
public class Stomach<T extends Food> implements Consumer<T> { | |
@Override | |
public void accept (T t) { | |
t.giveEnergy (); | |
} | |
} | |
package com.company.samples.way6; | |
public class Main { | |
public static void main (String[] args) { | |
Stomach stomach = new Stomach (); | |
stomach.accept (new Salad ()); | |
stomach.accept (new Burger ()); | |
} | |
} | |
Outputs::: | |
Salad does not give energy | |
Burger gives high energy | |
No comments:
Post a Comment