1

I am porting some OpenGL Nvidia C samples to jogl and I have the following (init is one of the abstract methods required by GLEventListener:

public abstract class NvAppBase implements GLEventListener {
    @Override
    public void init(GLAutoDrawable drawable) {
        initRendering(gl4);
    }
    public void initRendering(GL4 gl4) {
    }
}

public abstract class NvSampleApp extends NvAppBase {
    @Override
    public void init(GLAutoDrawable drawable) {
        baseInitRendering(gl4);
    }
    protected void baseInitRendering(GL4 gl4) {
        initRendering(gl4);
    }
    @Override
    public void initRendering(GL4 gl4) {
    }
}
public class BindlessApp extends NvSampleApp{    
    @Override
    public void initRendering(GL4 gl4) {    
    }
}

Given that:

  • NvAppBase is not used at all, all the samples (such as BindlessApp) always extend NvSampleApp
  • I'd like the class extending NvSampleApp to being able to see (and overwrite) only the initRendering and not also the init

Is there a better way than just having NvSampleApp simply as a variable inside BindlessApp, like this for example?

public class BindlessApp {    
    private NvSampleApp sampleApp;
}
elect
  • 6,765
  • 10
  • 53
  • 119

2 Answers2

3

You can use the keyword final for this purpose.

Writing Final Classes and Methods on Oracle java tutorial.

You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.

Ferrybig
  • 18,194
  • 6
  • 57
  • 79
1

Is there a better way than just having NvSampleApp simply as a variable inside BindlessApp, like this for example?

Although it seems like more work, encapsulation is a great tool to help isolate parts of your code an decrease coupling.

I think in your case it might even be the better solution :)

See for more detail this answer: https://stackoverflow.com/a/18301036/461499

Community
  • 1
  • 1
Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121