Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (784)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (858)
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 925 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  
 
 

 
hadezbladez (1643 views)
2018-11-16 13:46:03

hadezbladez (659 views)
2018-11-16 13:41:33

hadezbladez (1638 views)
2018-11-16 13:35:35

hadezbladez (346 views)
2018-11-16 13:32:03

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

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

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

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

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

nelsongames (2971 views)
2018-04-24 18:14:32
Deployment and Packaging
by philfrei
2019-02-17 20:25:53

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
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!