Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Generics problem when extending  (Read 875 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2014-03-08 14:44:32 »

I have a particle system class:
1  
2  
3  
public class ParticleSystem<P extends Particle>{
    ...
}


I then try to extend this class with an internal ParticleImpl class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public class BasicParticleSystem extends ParticleSystem<ParticleImpl>{ // <-- Error

    ...

    private class ParticleImpl extends Particle{
        ...
    }

}

However, the compiler complains that "ParticleImpl cannot be resolved to a type". Eclipse proposes importing the class manually, but does not do anything when I choose that option.


EDIT:
I found a solution. Manually importing the inner class and changing the visibility makes it compile:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
import blah.BasicParticleSystem.ParticleImpl;

public class BasicParticleSystem extends ParticleSystem<ParticleImpl>{

    ...

    class ParticleImpl extends Particle{

        ...

    }
}
It's still weird as hell.

Myomyomyo.
Offline UprightPath
« Reply #1 - Posted 2014-03-08 16:57:11 »

I think it's an issue of inner classes typically being considered part of the outer class so its visibility is sort of wonky. So you have to explicitly make it visible so that the compiler can check whether it matches the Generic's constraints.

Edit: I misunderstood the problem when I wrote my response. Changed it complety! xD

Offline TeamworkGuy2

Junior Devvie


Medals: 10



« Reply #2 - Posted 2014-03-08 18:34:28 »

UprightPath is correct, it's a visibility problem, if you want to skip the import statement you can use the inner class's full name like so:

1  
2  
3  
4  
public class BasicParticleSystem extends ParticleSystem<BasicParticleSystem.ParticleImpl> {
   class ParticleImpl extends Particle {
   }
}
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1574 views)
2018-06-10 19:43:48

EgonOlsen (1658 views)
2018-06-10 19:43:44

EgonOlsen (1145 views)
2018-06-10 19:43:20

DesertCoockie (1573 views)
2018-05-13 18:23:11

nelsongames (1174 views)
2018-04-24 18:15:36

nelsongames (1665 views)
2018-04-24 18:14:32

ivj94 (2421 views)
2018-03-24 14:47:39

ivj94 (1631 views)
2018-03-24 14:46:31

ivj94 (2719 views)
2018-03-24 14:43:53

Solater (884 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!