473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Generic Classes in Java

sreekandank
Generic class is a class with one or more type variables. A Generic class declaration looks like a non-generic class declaration, except that the class name followed by a type parameter. For instance, consider a following generic class Employee<T>.

Expand|Select|Wrap|Line Numbers
  1. class Employee<T>
  2. {
  3.  private T obj;
  4.  Employee(T obj)
  5.  {
  6.   this.obj=obj;
  7.  }
  8.  T getObj()
  9.  {
  10.   return obj;
  11.  }
  12. }
  13.  
The Employee class introduces a Type variable T, enclosed in angle brackets <>, after the class name. The type variables can be used throughout the class definition to specify method return types, and the types of variables.

A generic class can have more than one type variables. For example,
class Employee<T,U>

Let's consider the following example program that shows the generic Employee class, and it uses the Type variable T.

Expand|Select|Wrap|Line Numbers
  1. class Employee<T>
  2. {
  3.  private T obj;
  4.  Employee(T obj)
  5.  {
  6.   this.obj=obj;
  7.  }
  8.  T getObj()
  9.  {
  10.   return obj;
  11.  }
  12. }
  13. /**
  14. *@author Sreekandan.K
  15. */
  16. public class GenericEmployeeClass
  17. {
  18.  public static void main(String args[])
  19.  {
  20.   Employee<String> str=new Employee<String>("AAS");
  21.   System.out.println("Name:"+str.getObj());
  22.   Employee<Integer> i=new Employee<Integer>(21);
  23.   System.out.println("Age:"+i.getObj());
  24.   Employee<Double> d=new Employee<Double>(30000.0);
  25.   System.out.println("Salary:"+d.getObj());
  26.  }
  27. }
  28.  
Oct 31 '12 #1
0 15728

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Johnas Owan | last post by:
Hi! I'd like to build xml out of some c# classes with the XMLSerializer. My problem is: I have an envelope and want to fill it with different types of sub-nodes. But I don't like to build the...
1
by: Rod Bass | last post by:
I was disappointed to see that you can't use a parameter type as a base class for a generic class. I would find it extremely useful to extend a generic type of class (as is done nicely using C++...
6
by: Robin | last post by:
Are there any generic classes available that can be used in VB.Net to perform common tasks such as Database Access and Logging?
3
by: Levi | last post by:
class A<Ta> { public A() { } } class B<TA, Ta> where TA : A<Ta> { public B() { } }
3
by: Anatoli Koutsevol | last post by:
Is there some way to determine size in bytes for a type parameter T in generic class cls<T>. For instance, I need some generic way to covert T array to an array of bytes and conversely, as in...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
3
by: =?Utf-8?B?Sm9hcXVpbg==?= | last post by:
OK, this was hard to explain on a short title. Sorry. I have an abstract class (MustInherit) named Base, and a generic collection class named ColBase which can only take Base and derived classes...
5
by: Torben Laursen | last post by:
I am writing a COM in C# using visual studio 2005 and VSTO. Inside the code I use some support classes that are generic but they are not used in the inferface of the COM. However I still get a...
2
by: =?Utf-8?B?Sm9obk1TeXJhc29mdA==?= | last post by:
I have a generic class defined. My "server" code wants to call methods on the generic class after I've assigned the type to it. I was trying to do this via a SELECT CASE statement but I can't...
1
by: raylopez99 | last post by:
Here is an example of a home grown generic class, representing a pair of values. Adapted from Jon Skeet's book "C# In Depth". The generic class is "sealed" for some reason (I think for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.