Results 1 to 3 of 3
- 10-15-2012, 10:07 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 18
- Rep Power
- 0
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
- 10-15-2012, 11:05 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 787
- Rep Power
- 11
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!
- 10-15-2012, 11:44 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
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
-
call default constructor inside of parameterized constructor after conditional check
By Googol in forum New To JavaReplies: 5Last Post: 08-11-2012, 10:50 AM -
How to obtain a caller object reference from within a called method?
By mayngontay in forum Advanced JavaReplies: 8Last Post: 04-20-2011, 06:02 AM -
y superclass constructor is called automatically
By _ShivamKapoOr_ in forum New To JavaReplies: 1Last Post: 09-24-2010, 02:58 PM -
ISSUE: Array as class constructor will not copy elements, only reference
By Lucificate in forum New To JavaReplies: 16Last Post: 07-08-2010, 10:13 PM -
My constructor not called
By rdtindsm in forum New To JavaReplies: 2Last Post: 09-20-2009, 02:38 AM
Bookmarks