Results 1 to 3 of 3
  1. #1
    Akirien is offline Member
    Join Date
    Feb 2012
    Posts
    18
    Rep Power
    0

    Default Cannot reference before supertype constructor has been called..

    Can anyone help me with my code? I'm getting the error "cannot reference manufacturer before supertype constructor has been called and cannot reference weight before supertype constructor has been called.

    Java Code:
    public abstract class Computer {
    	
    	public String manufacturer;
    	public double weight;
    	
    	public Computer(String manufacturer, double weight){
    		
    		this.manufacturer = manufacturer;
    		this.weight = weight;
    	}
    	
    	public abstract void description();
    
    
    public class Desktop extends Computer{
    	
    	public String modelNo;
    	public String processorType;
    	
    	public Desktop(String model, String processor){
    		
    		super(manufacturer, weight);
    		modelNo = model;
    		processorType = processor;
    		
    		
    	}
    		
    		public void description()
    		{
    			
    			System.out.println("Description : " +modelNo);
    			System.out.println("Processor : " +processorType);
    			System.out.println("Weight : " +weight);
    			
    		}
    	
    
    
    public class TestComputer{
    	
    	public static void main(String[] args){
    		
    		Desktop desk = new Desktop("Dell345", "2 Kilogram");
    		
    		desk.description();
    	}

    My head is already burning. Thank you.. :D

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Cannot reference before supertype constructor has been called..

    Your code super(manufacturer, weight); makes no sense! What do you want to do?
    With super(...) you are trying to call the super constructor -> the constructor of computer, which is public Computer(String manufacturer, double weight){ So you have to pass a string and a double.

    So it would make sense if the constructor of Desktop needs four arguments, or you would pass default values to the super constructor!

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Cannot reference before supertype constructor has been called..

    Also remember that a Desktop IS-A Computer. So that, in particular, a desktop will also have a manufacturer and weight just like any other computer. When you call the Desktop constructor perhaps you should pass this information as well.

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 10:50 AM
  2. Replies: 8
    Last Post: 04-20-2011, 06:02 AM
  3. y superclass constructor is called automatically
    By _ShivamKapoOr_ in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 02:58 PM
  4. Replies: 16
    Last Post: 07-08-2010, 10:13 PM
  5. My constructor not called
    By rdtindsm in forum New To Java
    Replies: 2
    Last Post: 09-20-2009, 02:38 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
  •