custom generic classes

Hi,

I was reading below article


http://www.thejavageek.com/2013/08/29/generics-creating-custom-generic-classes/

I was not clear on what is 'custom generic classes' when, where, how to use them. Please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
A custom generic class is anytime you write a class that starts like this:

public class Library<T>

rather than just

public class Library.

When to use them?  When you have a class that works with a range of other classes.  The simplest example is a List.  Lists work with other classes (the types they store).  So if you were writing a new improved list, you would make it generic like this:

public class MyBetterList<T> {
   private List<T> myInnerList ;
}

rather than writing:
public class MyBetterListInt {
   private List<Integer> myInnerList ;
}
public class MyBetterListString {
   private List<String> myInnerList ;
}
etc.

Make sense?

Doug
0
zzynxSoftware engineerCommented:
The link you provided is rather clear:

- Consider a class Library.
- Now if we want to make a library of books, then we need to extends this class:
    public class BookLibrary extends Library
- What if 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:
       public class Library<T> { // "T" is the Type parameter. We can create Library of any Type
0
gudii9Author Commented:
public class MyBetterListInt {
   private List<Integer> myInnerList ;
}
public class MyBetterListString {
   private List<String> myInnerList ;
}

class name is just name right whether int or string like MyBetterListInt  and MyBetterListString
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

gudii9Author Commented:
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.

what is meaning of generic way of representing library of books, CD, magazines etc.

Any other simple complete examples on this. Please advise
0
zzynxSoftware engineerCommented:
>> what is meaning of generic way of representing library of books, CD, magazines etc.

You don't want the create separate classes for each of that different library.
Those classes have too much in common.
Eg. take the next book, take the next CD, take the next magazine.
In the general class we can make a method "take the next library item".
0
dpearsonCommented:
class name is just name right whether int or string like MyBetterListInt  and MyBetterListString

Yes a class name is any string of characters.  It's just a name.

It's only when there are < > characters involved that there's an actual type involved:

List<Integer>       (Integer is another Java class)
vs
ListOfWholeNumbers  ("WholeNumbers" is just a name, nothing to do with any other class)

Doug
0
gudii9Author Commented:
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.thejavageek.generics;
 
import java.util.ArrayList;
import java.util.List;
 
public class TestLibrary {
 
	public static void main(String[] args) {
 
		List<Book> books = new ArrayList<Book>();
		books.add(new Book());
		books.add(new Book());
		books.add(new Book());
 
		Library<Book> bookLibrary = new Library<Book>(books);
 
		Book book = bookLibrary.issueItem();
		System.out.println(book);		
		List<CD> cds = new ArrayList<CD>();
		cds.add(new CD());
		cds.add(new CD());
		cds.add(new CD());
 
		Library<CD> cdLibrary = new Library<CD>(cds);
 
		CD cd = cdLibrary.issueItem();
		System.out.println(cd);
	}
 
}

Open in new window



in below code

     List<Book> books = new ArrayList<Book>();
            books.add(new Book());
            books.add(new Book());
            books.add(new Book());
 
            Library<Book> bookLibrary = new Library<Book>(books);

what are we doing.

what is difference between below lines
            Library<Book> bookLibrary = new Library<Book>(books);

and
List<Book> books = new ArrayList<Book>();

are not we just creating new Book objects and then adding to array list or Library object?
0
gudii9Author Commented:
one is arraylist constructor and other is constriuctor for Library generic class as below?

Library<Book> bookLibrary = new Library<Book>(books);
0
gudii9Author Commented:
Library<Book> bookLibrary = new Library<Book>(books);

Open in new window


is above line is constructor?

it looks new thing to me?
Esp why they say (book) at the end?
please advise
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
is above line is constructor?

Yes, it is a constructor

Esp why they say (book) at the end?

Because that is a parameter that is passed to the Library constructor. (Have a look at line 9 of the third code block in the link above, the one that defines the Generic Library)

So, what we are doing is creating a new Library object, this new Library is a Library of Book objects and we are passing it an initial list of "books" to start managing.

Does that make sense?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dpearsonCommented:
It's also important to think about 2 of these at the same time - otherwise the whole "generic type" thing won't make any sense.

For instance you can have:
Library<Book> bookLibrary = new Library<Book>(books);
Library<CD> audioLibrary = new Library<CD>(cds);

The idea being that you want to have a library of books and a library of cds and both share a lot in common (e.g. you can look up a specific title by name).  That shared code would be in Library.

Doug
0
gudii9Author Commented:
Because that is a parameter that is passed to the Library constructor. (Have a look at line 9 of the third code block in the link above, the one that defines the Generic Library)
i see it

public Library(List<T> items){
            this.items = items;
      }

library constructor does take item parameter
0
gudii9Author Commented:
i wonder in real time i hardly ever saw these kind of generic classes yet?
0
zzynxSoftware engineerCommented:
That depends on how "deep" you're involved in to real coding projects of course...
Indeed, I can't say I encounter them on a daily base, but they certainly have their advantages in certain circumstances.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.