• 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Generics ... help ! ?

 
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have a question that has been stumping for about 5 hours !

I want to extend a generic, abstract class...
I cant figure out how to do so, however, in such a
way that the inherited method has a strongly typed
parameter. Ive made an example that can be tested pasted
easily into eclipse

class MyExample
{
static abstract class GenericAbstract <A extends Object>
{
public abstract void doSomething(A a);
}

static class A { }
static class B extends A { }

/** Why cant I simply extend the generic abstract by
* concretely declaring B ? Also - why is it that
doSomething does not override properly ? Isnt that
the point of generics - to allow dynamic definition
of types ? If so - it should let me override by extension.
*
*/
static class ClassThatWontCompile <B> extends GenericAbstract
{
public void doSomething(B b)
{


}

}
}
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a compiler where I am now, but I believe this syntax should work:



 
Marshal
Posts: 27586
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
 
author
Posts: 23936
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since Generics are core to Java -- not an external API... moving this to Java in General (Intermediate).

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

Originally posted by jay vas:
Hi I have a question that has been stumping for about 5 hours !

I want to extend a generic, abstract class...
I cant figure out how to do so, however, in such a
way that the inherited method has a strongly typed
parameter. Ive made an example that can be tested pasted
easily into eclipse

class MyExample
{
static abstract class GenericAbstract <A extends Object>
{
public abstract void doSomething(A a);
}

static class A { }
static class B extends A { }

/** Why cant I simply extend the generic abstract by
* concretely declaring B ? Also - why is it that
doSomething does not override properly ? Isnt that
the point of generics - to allow dynamic definition
of types ? If so - it should let me override by extension.
*
*/
static class ClassThatWontCompile <B> extends GenericAbstract
{
public void doSomething(B b)
{


}

}
}



I think you are just mixing the type parameter used in the declaration of GenericAbstract and ClassThatWontCompile with class A and B respectively.

Your declaration of GenericAbstract is :



Here A is not the nested class A. Rather its just a type parameter.

Similary in the declaration of ClassThatWontCompile:



Type parameter B is not the nested class B. Rather agaian its a type parameter. You can use anything here X or Y etc.

When you actually use your class, then you need to provide the type argument, which is in your case nested class A and B.


Is this you are looking for?




Naseem
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ! I figured it out right before you guys left.

Generics are awesome once you finally figure them out.

I know alot of people complain that they are overwhelming and wordy, but
in the age of high power IDEs, wordyness is not a big deal - because its still faster than tracking down an unsafe cast.
 
He's giving us the slip! Quick! Grab this tiny ad!
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic