Using an interface as a parameter

You can define methods that take an interface as a parameter. Your interface defines a contract and your methods will accept as parameter any objects whose class implements that interface. This is in fact one of the most common and useful ways to use an interface. interface Test { public void test(); //define the interface } class Tester { public void runTest(Test t) { t.test(); } // method with interface as param } MyTest class will implement this interface:...

April 20, 2017 · 1 min · Rezha Julio

Using bounded type parameters in generic methods

Sometimes it may be appropriate to write a generic method, however it will not be possible for it to accept every type while still maintaining all the necessary functionality. To solve this, use bounded type parameters to restrict generic methods from accepting arguments of a particular kind. public <T extends Shape> void drawAll(List<T> shapes){ for (Shape s: shapes) { s.draw(this); } } The above method is used to draw a list of shapes....

April 19, 2017 · 1 min · Rezha Julio