Overriding a method question.

Hello, and thanks for reading this.
I’m pretty new to Java development and i caught myself here in this AWESOME ;D site were things seems to work better than trying every thing all alone.
I am trying to use a game base engine that i found here in the tutorial section but for some reason i can’t override any class. I create the base class and a Game main class to place the game code separately, than i extended the Game class to the base class this way i could override the update method and the render method. For some weird reason none of those are accepting my overrides :’( .

The code:

---------------------The (modified) Base class

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Base implements Runnable{
	
	int WIDTH = 1024;
	int HEIGHT = 768;
	
	JFrame frame;
	Canvas canvas;
	BufferStrategy bufferStrategy;
	
	private GraphicsDevice  gd;
	private DisplayMode defaultDM;
	
	public static final DisplayMode DefaultDisplaySet[]={
		//640x480
		new DisplayMode(640,480,32,0),
		new DisplayMode(640,480,16,0),
		//800x600
		new DisplayMode(800,600,32,0),
		new DisplayMode(800,600,16,0),
		//1024x768
		new DisplayMode(1024,768,32,0),
		new DisplayMode(1024,768,16,0),
		//1440x900
		new DisplayMode(1440,900,32,0),
		new DisplayMode(1440,900,16,0)
	};
	
	public Base(){
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		gd = env.getDefaultScreenDevice();
		defaultDM = gd.getDisplayMode();
		
		frame = new JFrame("Basic Game");
		frame.setUndecorated(true);
        gd.setFullScreenWindow(frame);
        if (gd.isDisplayChangeSupported())
        {
            gd.setDisplayMode(checkCompatibleMode(DefaultDisplaySet[7]));
            this.WIDTH = gd.getDisplayMode().getWidth();
            this.HEIGHT = gd.getDisplayMode().getHeight();
        }
          
		JPanel panel = (JPanel) frame.getContentPane();
		panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
		panel.setLayout(null);
		
		canvas = new Canvas();
		canvas.setBounds(0, 0, WIDTH, HEIGHT);
		canvas.setIgnoreRepaint(true);
		
		panel.add(canvas);
		
		canvas.addMouseListener(new MouseControl());
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setResizable(false);
		frame.setVisible(true);
		
		initialize();
		
		canvas.createBufferStrategy(2);
		bufferStrategy = canvas.getBufferStrategy();
		
		canvas.requestFocus();
	}
	
	//diplay mode handlers
	public DisplayMode[] getCompatibleDisplayModes(){
		return gd.getDisplayModes();
	}
	
	public DisplayMode checkCompatibleMode(DisplayMode mode){
		DisplayMode goodModes[] = getCompatibleDisplayModes();
			for(int j=0;j<goodModes.length;j++){
				if (DisplayModesMatch(mode,goodModes[j])){
					return mode;
				}
		}
		return null;
	}
	
	//compara displaymodes
	public boolean DisplayModesMatch(DisplayMode d1, DisplayMode d2){
		if(d1.getWidth() != d2.getWidth() || d1.getHeight() != d2.getHeight()){
			return false;
		}
		if(d1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && d2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && d1.getBitDepth() != d2.getBitDepth()){
			return false;
		}
		if(d1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && d2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && d1.getRefreshRate() != d2.getRefreshRate()){
			return false;
		}
		
		return true;
	}
	
	
	//initialize - override
	
	protected void initialize(){}
	
	//the keyInput for shutting down
	
	private class KeyInputHandler extends KeyAdapter
	{
	                public void keyPressed(KeyEvent e)
	                {
	                        if(e.getKeyCode()==27)
	                {
	                                gd.setDisplayMode(defaultDM);
	                                gd.setFullScreenWindow(null);
	                        System.exit(0);
	                }
	                }
	               
	                public void keyReleased(KeyEvent e)
	                {
	                       
	                }
	}
	
	//the mouse listeners
	
	private class MouseControl extends MouseAdapter{
		
	}
	
	long desiredFPS = 100;
    long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
    
	boolean running = true;
	
	//the separate thread func
	
	public void run(){
		
		long beginLoopTime;
		long endLoopTime;
		long currentUpdateTime = System.nanoTime();
		long lastUpdateTime;
		long deltaLoop;
		
		while(running){
			beginLoopTime = System.nanoTime();
			
			render();
			
			lastUpdateTime = currentUpdateTime;
			currentUpdateTime = System.nanoTime();
			update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
			
			endLoopTime = System.nanoTime();
			deltaLoop = endLoopTime - beginLoopTime;
	        
	        if(deltaLoop > desiredDeltaLoop){
	            //Do nothing. We are already late.
	        }else{
	            try{
	                Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
	            }catch(InterruptedException e){
	                //Do nothing
	            }
	        }
		}
	}
	
	//rendering
	
	private void render() {
		Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
		g.clearRect(0, 0, WIDTH, HEIGHT);
		render(g);
		g.dispose();
		bufferStrategy.show();
	}

	int counter = 0;
	
	//updating - overriding
	
	public void update(int deltaTime){
	}
	
	//rendering - overriding
	
	public void render(Graphics2D g){
		g.setColor(Color.black);
		g.fillRect(0, 0, this.WIDTH, this.HEIGHT);
	}
	
	//main thread func
	
	public static void main(String [] args){
		Base ex = new Base();
		new Thread(ex).start();
	}
	
}

---------------------The Main Game Class

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

public class Game extends Base{
	
	BufferedImage bg = null;
	int counter = 0;
	
	protected void initialize(){
	try {
	    bg = ImageIO.read(new File("bg.png"));
	} catch (IOException e) {}
	}
	
	public void update(int deltaTime){
		counter++;
		if (counter > 100){
			counter = 0;
			JOptionPane.showMessageDialog(null, "UPDATE FUNCIONANDO."); //NOT WORKING
		}
	}
	public void render(Graphics2D g){
		g.setColor(Color.blue);
		g.fillRect(0, 0, 1024, 768); //NOT WORKING EITHER
		//g.drawImage(bg, 0, 0, canvas);
	}

}

That should be working, we’d have to see more code. Are you instantiating a Base or a Game class?


public class SuperClass
{
    public void doSomething()
    {
        System.out.println("SuperClass is printing.");
    }
}

public class SubClass extends SuperClass
{
    public void doSomething()
    {
        System.out.println("SubClass is printing.");
    }
}

public class DoStuffClass
{
    public static void main(String[] args)
    {
        SuperClass myClass = new SuperClass();
        myClass.doSomething(); //Prints "SuperClass is printing."

        myClass = new SubClass();
        myClass.doSomething(); //Prints "SubClass is printing."

        SubClass myOtherClass = new SubClass();
        myOtherClass.doSomething(); //Prints "SubClass is printing."

        //This is illegal:
        SubClass myThirdClass = new SuperClass();
        myThirdClass.doSomething(); //Illegal
    }
}

So have a look at the DoStuffClass, and make sure you’re instantiating your class in the correct way. As long as you’re calling new Game(), the correct methods should be getting called. But if you call new Base(), then you’ll still be getting stuff from the Base class.

Forgive me if this was obvious to you, but it’s the only way that I can see you getting no overriding. Or if your method signatures were not the same, but I can see above that they are.

Well, thats all my code so far… I’m running(calling) the Game class where I extend the base class.

Sorry, I should have looked further down. You did exactly what I said would be a problem:


public static void main(String [] args){
	Base ex = new Base();
	new Thread(ex).start();
}

If you call a new Base(), you are making a new Base class, not a new Game class. You want to do:


public static void main(String [] args){
	Base ex = new Game();
	new Thread(ex).start();
}

or


public static void main(String [] args){
	Game ex = new Game();
	new Thread(ex).start();
}

It’s all about which object you are actually instantiating.

MAN! I LUV YOU! Thank you so much!

No problem. :slight_smile: