Results 1 to 3 of 3
Like Tree2Likes
  • 1 Post By DeNiS_M
  • 1 Post By Fubarable

Thread: Java Class Inheritance Problem

  1. #1
    Batman is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Post Java Class Inheritance Problem

    Hello,

    I have a problem with some work I'm doing at the moment that involves having a Superclass and a subclass, which are can be called from
    the main 'Renderer' class. I want to be able to have a few of the variables from the Renderer class in these other classes namely the
    Superclass.

    Ok the superclass is:
    Java Code:
    import java.awt.*;
    
    public class Building{
        
        int Tile_x;
        int Tile_y;
        int Health;
        
        Image Picture;
        
        
        Renderer p;
    
        public Building(Renderer parent) {
            p = parent;
        }
        
        
        public void inital(Image temp_image, int Temp_Tile_x, int Temp_Tile_y, int temp_Health){
            
            Tile_x = Temp_Tile_x;
            Tile_y = Temp_Tile_y;
            Picture = temp_image;
            Health = temp_Health;
            
        }
        
    //    public void draw(){
    //        p.Buffer.drawImage(Picture, p.Tiles[Tile_x][Tile_y].x, p.Tiles[Tile_x][Tile_y].y, this);
    //    }
        
    }

    and the subclass

    Java Code:
    public class Fortification extends Building{
        
    
        public void tick(){
            
            //does nothing for the moment
            
        }
    }
    Now the problem lies where in the subclass I get red underlines (NetBeans) saying:


    Building in class Building cannot be applied to given types
    Required: Renderer
    Found: no arguments
    Reason: actual and formal argument list differ in length

    would be great if someone could help

    Regards Batman

  2. #2
    DeNiS_M is offline Member
    Join Date
    Sep 2011
    Location
    Athens Greece
    Posts
    29
    Rep Power
    0

    Default Re: Java Class Inheritance Problem

    I think you should add a constructor in the Fortification class taking a Render object as parameter!
    Fubarable likes this.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Java Class Inheritance Problem

    I agree with Denis, but more: the Fortification class's constructor needs to call the super (Building) class's constructor in its first line, and needs to pass the Renderer parameter into the super constructor call:

    Java Code:
    public Fortification(Renderer renderer) {
       super(renderer); // *** here ***
    }
    This is because every class's constructor always calls its super class's constructor either explicitly where you call super(...) as I show above, or implicitly where you don't call the super(...). If it is done implicitly, it tries to call the super class's default constructor, the one with no parameters, but this doesn't exist for Building and will cause an compilation error to occur.
    DeNiS_M likes this.

Similar Threads

  1. Class Inheritance Cast
    By letdoit in forum New To Java
    Replies: 1
    Last Post: 02-03-2011, 06:14 AM
  2. Class inheritance question (code provided)
    By Adomini in forum New To Java
    Replies: 8
    Last Post: 10-09-2010, 05:30 AM
  3. how to get the child class in inheritance?
    By java_fun2007 in forum New To Java
    Replies: 7
    Last Post: 09-29-2010, 09:35 AM
  4. Replies: 14
    Last Post: 06-17-2010, 03:47 PM
  5. inheritance problem
    By er1c550n20 in forum New To Java
    Replies: 2
    Last Post: 03-10-2010, 06:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •