Results 1 to 4 of 4
  1. #1
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Correct use of "extends"

    So i have this class named VisibleObj:

    Java Code:
    package engine.obj;
    
    import java.awt.image.BufferedImage;
    
    public class VisibleObj {
    	//----------------------------------------------------------------------//
    
    	public int posX;
    	public int posY;
    	public int height;
    	public int width;
    	public BufferedImage sprite;
    	
    	//////////////complete constructor////////
    	public VisibleObj(int x, int y, int h, int w, BufferedImage s){
    		this.posX = x;
    		this.posY = y;
    		this.height = h;
    		this.width = w;
    		this.sprite = s;
    	}
    	
    	//////////////pos-size constructor////////
    	public VisibleObj(int x, int y, int h, int w){
    		this.posX = x;
    		this.posY = y;
    		this.height = h;
    		this.width = w;
    	}
    	
    	/////////////pos-only constructor/////////
    	public VisibleObj(int x, int y){
    		this.posX = x;
    		this.posY = y;
    	}
    	
    	
    }
    and i want to make several other classes, all of them having at least those 3 constructors (maybe more) so i guess that the proper way to do it is by using "extends". This is one of the classes:

    Java Code:
    package engine.obj.env;
    
    import engine.obj.VisibleObj;
    
    public class Rock extends VisibleObj{
    	
    	public Rock(){
    	}
    }
    but it gives out a compilation error that says "Implicit super constructor VisibleObj() is undefined. Must explicitly invoke another constructor". I dont know what does that exactly mean. I think I'm not using subclasses very well...

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Correct use of "extends"

    When you call new Rock() then implicitly new VisibleObj() is called too. But the non args contructor for VisibleObj does not exist. Either you implement that or you explicitly call one of the existing VisibleObj contructors from the Rock constructor.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: Correct use of "extends"

    And what if i want a Rock constructor that depending on the integer that i use when calling it it will do a predefined construction. I mean this:

    Java Code:
    	public Rock(int a){
    		if(a==1){
    			super(1,2);
    		}
    		if(a==2){
    			super(2,4);
    		}
    		
    		...
    		
    	}

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Correct use of "extends"

    No chance. The super() call has to be the first thing you do in your constructor. What you can do is
    Java Code:
    public Rock(int a){
        super(1,2);
        if(a > 1){
           posX = a;
           posY = 2*a;
        }
         
        ...
         
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. "extends" and "implements"
    By newbie101 in forum New To Java
    Replies: 6
    Last Post: 02-27-2012, 02:12 PM
  4. Replies: 1
    Last Post: 10-03-2011, 03:42 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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