473,520 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generic Classes in Java

sreekandank
45 New Member
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 15738

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

Similar topics

1
3992
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 envelope class for each subsystem class again and again. Instead the type of sub-node shall be choosen during runtime. Is there a way to use class...
1
3973
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++ templates). For example: public class SoapClientProtocolWithHeaders<T>: T where T: System.Web.Services.Protocols.SoapHttpClientProtocol {...
6
3955
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
2222
by: Levi | last post by:
class A<Ta> { public A() { } } class B<TA, Ta> where TA : A<Ta> { public B() { } }
3
11969
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 BitConverter.GetBytes. To create a buffer I'd like to have something like this T srcData; byte buffer = new byte;
3
2749
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 dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from...
3
4261
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 as it's type. I also have an indefinite number of classes that inherit Base. I want to make a method on Base that returns a ColBase(of Base), but...
5
11107
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 number of warnings from the compiler like: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets : warning : Type library exporter...
2
1876
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 seem to determine how to declare the object at the module level (so I can use it in several methods) but assign the type when I create the instance. ...
1
2450
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 performance) so you cannot derive from it, but that's optional. The main thing in this example is to show the right format for using these generic classes,...
0
7238
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7470
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7210
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5769
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5159
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
857
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.