Thursday , December 12 2019

JAVA #28 Generics in Java

Generics in Java

In a Java, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while the inputs to type parameters are types.

File name ObjectMain.java
package object;
public class ObjectMain {

    public static void main(String[] args) {
           Memory<String> m = new Memory<String>();
           m.read(23.21+"Faheem");
           System.out.println(m.write());            
    }
    
}
class Memory<T>
{
    private T value;
    public T write() // Getter Function
    {
        return value;
    }
    public void read(T ob) // Setter Function
    {
        value = ob;
    }
}

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

Leave a Reply

Your email address will not be published.