Click here to watch in Youtube :
https://www.youtube.com/watch?v=VawfMZaSWJU&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge
Java Tutorial: Generics in java | Java Generics [Wildcard in Java Generics] |
public abstract class Shape { abstract void draw(); } class Circle extends Shape { void draw() { System.out.println("drawing circle"); } } class Rectangle extends Shape { void draw() { System.out.println("drawing rectangle"); } }GenericDemo.java
import java.util.ArrayList; import java.util.List; public class GenericDemo { public static void main(String args[]) { List<Rectangle> recList = new ArrayList<Rectangle>(); recList.add(new Rectangle()); recList.add(new Rectangle()); drawShapes(recList); List<Circle> circleList = new ArrayList<Circle>(); circleList.add(new Circle()); circleList.add(new Circle()); drawShapes(circleList); } /* * This method accepts only the list contains child class of Shape */ public static void drawShapes(List<? extends Shape> shapeList) { for (Shape shape : shapeList) { shape.draw(); } } }Output
drawing rectangle drawing rectangle drawing circle drawing circleClick the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/GenericsDemo-wildcard_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/GenericsDemo-wildcard_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/GenericsDemo-wildcard_App/?at=master
See also:
No comments:
Post a Comment