Generics : Creating custom generic classes

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all, In the previous part of this generics series, we learned about The WildCard Operator. In this part, we are going to see how to create custom generic classes.

But what to create custom generic classes?

Consider a Library. We can have a book library of audio library or cd library, whatever.Library class is.

Now if we want to make a library of books, then we need to extends this class.

We created BookLibrary but there are a lot of issues here.

  • We have to create a new class just to make it a library of books.
  • We have to check the type of Item in returnItem method as the overriding method parameter type must be that of superclass method.
  • What we we need to make libraries of books, cds, news papers, magazines and lots of them. Are we going to create a separate class for that? Of course not.

What we need is a generic way to do this. We need to create a custom generic class.

So we will create a generic library like this.

Let us create Library now

 

 

This outputs,

This means we can successfully create a generic type which can type any Type as argument and we can perform operations on it. We do not have to write Type specific methods. Generics handles everything.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

12 thoughts on “Generics : Creating custom generic classes

  • October 30, 2014 at 5:49 pm
    Permalink

    Good explanation !!

    Reply
  • July 1, 2015 at 3:03 pm
    Permalink

    Thanks for the post. It was very helpful.

    Reply
  • October 22, 2015 at 7:00 pm
    Permalink

    HI Prasad
    Your post on generics custom classes explained the concept in simple way.Thanks a lot.

    Reply
      • August 17, 2016 at 9:25 pm
        Permalink

        Could you please tell us why java is not allowing to create

        Object Ob=new T(); //Error

        Reply
  • May 26, 2016 at 1:54 pm
    Permalink

    Can you show code for Book and CD implementation?

    Reply
    • May 26, 2016 at 2:45 pm
      Permalink

      Hi Jagadeesh, those are simple Plain Old Java Objects, you can create your own Book and CD classes and try below code, I will help if some problem comes.

      Reply
      • June 16, 2016 at 6:25 pm
        Permalink

        Yep!! thanks for the help, can we use generics for implementing some common methods that can be implemented other than storing and retrieving objects?

        Reply
  • January 21, 2017 at 3:31 pm
    Permalink

    can u please tell how to create custom objects in java?

    Reply
  • January 21, 2017 at 3:32 pm
    Permalink

    can u please tell how to create the custom subjects in java

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *