Method Overriding - in Nashorn Javascript (js) Engine in Java 8




First Please Read :Method-overriding-in-java


Program/ Example of Nashorn Javascript (js) Engine in Java 8 - Method Overriding in javascript >

package nashornJs;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
*
* Method Overriding in javascript - And call it from java
*
*/
public class MyNashornClass {
   public static void main(String[] args) throws Exception {
      System.out.println("In java - START");
      
      ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
       engine.eval("load('E:/pocJmse/myNashornJavaScript.js')");    
       System.out.println("In java - FINISH");
      
   }
}
/* OUTPUT
In java - START
In myNashornJavaScript.js
In js - 1) Method Overriding
Lion eat - flesh
In js - 2) Another way of doing above
Lion eat - flesh
In js - 3) Method Overriding - Implement java Animal in javascript
Lion eat - flesh
End myNashornJavaScript.js
In java - FINISH


*/

Interface - Animal
package nashornJs;
/*
* Interface - Animal
*/
public interface Animals {
   void food() ;
}

subclass of Animal interface - Lion
package nashornJs;
/*
* subclass of Animal interface - Lion
*/
public class Lion implements Animals {
   @Override
   public void food() {
       System.out.println("Lion eat - flesh");
   }
}


E:/pocJmse/myNashornJavaScript.js looks like this >
print('In myNashornJavaScript.js');
// 1) Method Overriding in js -
print('In js - 1) Method Overriding ');
var javaAnimals = Java.type('nashornJs.Animals');
//Implement java Animal using anonymous inner class in javascript
var javaLion = new javaAnimals() {
   // Provide implementation of food method
   food: function() {
       print('Lion eat - flesh');
   }
};
javaLion.food();
// 2) Another way of doing above
print('In js - 2) Another way of doing above ');
var javaLion2 = Java.extend(javaAnimals, {
   // Provide implementation of run method
   food: function() {
       print('Lion eat - flesh');
   }
});
new javaLion2().food();
// 3) Method Overriding - Implement java Animal in javascript
print('In js - 3) Method Overriding - Implement java Animal in javascript ');
var javaAnimals1 = Java.type('nashornJs.Animals');
var javaLion1 = Java.type('nashornJs.Lion');
var javaLion1 = new javaLion1();
javaLion1.food();

print('End myNashornJavaScript.js');


In  java we can write this code below to achive method overriding >
            Animals lion = new Animals() {
                 @Override
                 public void food() {
                       System.out.println("Lion eat - flesh");
                 }
          };
          lion.food();
         
          //call method
          Animals lion1 = new Lion();
          lion1.food();             




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

eEdit
Must read for you :