• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Generic Interface Example

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Is it good example for Generic interface?
 
Saloon Keeper
Posts: 14260
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends what you want to illustrate. It looks like it would compile.

Note that you shouldn't use identifiers starting with a lower case letter for classes and interfaces. Actually, a isn't a good class/interface name to start with, because it's not very descriptive.

Are you having any doubts?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This looks more like a blog post (look at what I did) masked as a question.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This looks more like a blog post



No!
I want to be sure, I asked a Generic class and I write an example, But after I post here one friend told me using of generic is pointless in your example!
 
Matthew Cox
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

abalfazl hossein wrote:

This looks more like a blog post



No!
I want to be sure, I asked a Generic class and I write an example, But after I post here one friend told me using of generic is pointless in your example!



If that is the case then no harm no foul. It helps if you are more specific with the wording of your question IMO. As to whether or not it is pointless .... interfaces are used to provide a unified type and define behavior. If you don't intend to use this interface anywhere else other than a specific class, then it could be deemed as pointless.

If we are assessing whether it is pointless to include a generic type in your interface ... then your friend is incorrect. You are creating a definition of behavior that can be enforced and checked at compile-time while allowing the flexibility of passing in generic types .. I don't see how that functionality is pointless.

To elaborate, I may want to utilize your interface to add people to my Classroom class, or maybe Teachers to my Faculty class and maintain the Classroom and Faculty instances referenced by the interface you defined. A bit contrived but a very common place theme as the problem size scales.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

From SCJP
Sun Certified Programmer
for Java® Platform, SE6


If you don't use generic here, This class works and only accept String. What is the usage of generic here?




I guess that it must be this:



Right? Because of "u"
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The generic cause more flexibility in this code,Not limitation of data type. Right?
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The usage of generic is type safe, and it reaches by limitation of data type. But in this example, We use any type of data in array. The I think it is not good idea for using generic, Right?
 
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not at all sure I understand the question. I agree with Matthew Cox about the way you have asked this question.

You can create any sort of array except a Foo<T>[]. You can however create a T[] in a class with a formal type parameter. So I don't understand why you are saying

I think it is not good idea for using generic, Right?

I don't think you are right, but don't really know what you are trying to say.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you use generic in this code:




Because you want to limit type of objects that can save in this array. You want only string saved.

But What about this code:




Every type of objects can be saved in this array, Because you use generic.

But the reason that we use generic is to limit the type of objects, Isn't?

That Is why I am saying it is not good example for generic.Unless There is another reason, Other than type safe and type limitation, in order to use generic.
 
Campbell Ritchie
Marshal
Posts: 76395
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks like a completely different question from what you asked at first. The idea behind your generic method (with the <E> in its heading) is that using methods can "tell" the method that they are going to use a particular type of array. You are right that you have given a poor example., but that is because you are using its toString() method, called implicitly by %s and printf. Because Object has a toString method, every possible object has a toString method. Let's alter that method of yoursYou will find the Engine interface here. Now you know you are restricting your array to contain Engines only, you can be confident in calling the start() stop() and useFuel() methods on everything in your array. You can pass Engine[] arrays, or PetrolEngine[] arrays, or Dieselengine[] arrays or ElectricEngine[] arrays.
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic