
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generic Constructors in Java
Generics is a concept in Java where you can enable a class, interface and, method, accept all (reference) types as parameters. In other words it is the concept which enables the users to choose the reference type that a method, constructor of a class accepts, dynamically. By defining a class as generic you are making it type-safe i.e. it can act up on any datatype.
To define a generic class you need to specify the type parameter you are using in the angle brackets “<>” after the class name and you can treat this as datatype of the instance variable an proceed with the code.
Example − Generic class
class Student<T>{ T age; Student(T age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } }
Usage − While instantiating the generic class you need to specify the object name after the class within the angular brackets. Thus, choose the type of the type parameter dynamically and pass the required object as a parameter.
public class GenericsExample { public static void main(String args[]) { Student<Float> std1 = new Student<Float>(25.5f); std1.display(); Student<String> std2 = new Student<String>("25"); std2.display(); Student<Integer> std3 = new Student<Integer>(25); std3.display(); } }
Example generic methods
Similar to generic classes you can also define generic methods in Java. These methods use their own type parameters. Just like local variables, the scope of the type parameters of the methods lies within the method.
While defining generic methods you need to specify the type parameter in the angular brackets and use it as a local variable.
public class GenericMethod { <T>void sampleMethod(T[] array) { for(int i=0; i<array.length; i++) { System.out.println(array[i]); } } public static void main(String args[]) { GenericMethod obj = new GenericMethod(); Integer intArray[] = {45, 26, 89, 96}; obj.sampleMethod(intArray); String stringArray[] = {"Krishna", "Raju", "Seema", "Geeta"}; obj.sampleMethod(stringArray); } }
Output
45 26 89 96 Krishna Raju Seema Geeta
Generic constructors
Constructors are similar to methods and just like generic methods we can also have generic constructors in Java though the class is non-generic.
Since the method does not have return type for generic constructors the type parameter should be placed after the public keyword and before its (class) name.
Once you define a generic constructor you can invoke (instantiate the class using that particular constructor) it by passing any type (object) as parameter.
Example
Following Java program demonstrates the usage of generic constructors in Java.
class Employee{ String data; public <T> Employee(T data){ this.data = data.toString(); } public void dsplay() { System.out.println("value: "+this.data); } } public class GenericConstructor { public static void main(String args[]) { Employee emp1 = new Employee("Raju"); emp1.dsplay(); Employee emp2 = new Employee(12548); emp2.dsplay(); } }
Output
value: Raju value: 12548