Tuesday, 16 May 2017

Java Tutorial: Java Generics[Define a generic method which accepts Type parameter with single bound]


Click here to watch in Youtube :
https://www.youtube.com/watch?v=ptvG7KBgTp8&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Java Generics[Define a generic method which accepts Type parameter with single bound] 
Box.java
public class Box<T>
{

    private T t;

    public void set(T t)
    {
        this.t = t;
    }

    public T get()
    {
        return t;
    }

    public <U extends Number> void inspect(U u)
    {
        System.out.println("T: " + t.getClass().getName());
        System.out.println("U: " + u.getClass().getName());
    }

}
GenericDemo.java
public class GenericDemo
{

    public static void main(String[] args)
    {
        Box<Integer> integerBox = new Box<Integer>();
        integerBox.set(new Integer(100));
        integerBox.inspect(new Integer(200));
        System.out.println("-------------------");
        integerBox.inspect(new Double(20.23));
        System.out.println("-------------------");
        integerBox.inspect(new Long(2000));

        /*
         * error: this is still String!
         */
        //integerBox.inspect("some text");

    }

}
Output
T: java.lang.Integer
U: java.lang.Integer
-------------------
T: java.lang.Integer
U: java.lang.Double
-------------------
T: java.lang.Integer
U: java.lang.Long
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/GenericsDemo_Bounded_TP_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/GenericsDemo_Bounded_TP_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/539015917c8406e4be671410d713073b3971d1a5/BasicJava/GenericsDemo_Bounded_TP_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge