Results 1 to 6 of 6
- 07-27-2010, 02:33 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
overridding constructor causes can not find symbol
i have an abstract class with a constructor
public abstract class Ticket <AnyType>{
public AnyType price;
public Ticket(AnyType p){
price =p;}
public abstract AnyType read();
}
______________________________________________
I want to extend the class and override the constructor
public class advancedTicket extends Ticket<Double>{
private static Double price;
private int serial;
private static int count;
public advancedTicket(double daysOut, double p){
if (daysOut > 10)
price = p * .75;
if(daysOut<=10)
price=p * .90;
serial=++count;
}
}
__________________________________________________ ___________
when i run main... something like
Ticket<Double>b = new advancedTicket(11.00, 25.00);
___________________________________
i get the following error... can anyone please tell me how I can over ride the constructor?
./advancedTicket.java:5: cannot find symbol
symbol : constructor Ticket()
location: class Ticket<java.lang.Double>
public advancedTicket(double daysOut, double p){
-
If your parent class declares a single constructor that takes parameters, the child class must call the parent's super constructor as the first call in its constructor. Likely you will pass in the p or price parameter into super. Why are you declaring a static field price in the child class anyway? Are advancedTicket objects going to have the exact same price no matter the location or event? This seems like another mistake you'll want to fix. Likely you'll not want to give advancedTicket a price field since the the parent class already has one.
-
Just fyi, whenever a child constructor is called, it always first calls the parent constructor. If no parent constructor is explicitly called via super(...) in the first line of the child's constructor than the default parameterless super constructor is called, here super() or Ticket(). But since Ticket has a constructor with a parameter, and doesn't declare a parameterless constructor as well, the compiler will rightly complain. Again, you'll want to call super(p) as the first line of the child constructor and get rid of that static Double price field in the child class.
edit: actually super(p * 0.75);
- 07-27-2010, 06:05 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
just to make sure i understand....so i cant have both a super(p) and super(a,b) correct?
- 07-27-2010, 09:54 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,479
- Rep Power
- 16
You can, but your parebnt (ie the "super" in question) doesn't have a constructor that takes two arguments, it has a constructor that takes one.
When an object is constructed the parent constructor is called first. If you haven't specifically written "super(whatever);" the compiler assumes you are calling the default constructor, which takes no parameters. However, the default only exists so long as no other constructor has been written...so your parent (Ticket<AnyType>) class no longer has a default constructor.
- 07-27-2010, 10:36 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
The rules are not so difficult:
1) a constructor either calls one of its other constructors or
2) it explicitly calls one of the constructors from its direct superclass or
3) it implicitly calls the no-args constructor from its direct superclass.
3) is normally generated by the compiler. A superclass constructor is called as super( ... ); another constructor in the same class is called as this( ... );
1) can cause a cycle, as in:
... and the compiler will notice it (it's not that stupid)Java Code:class X { X() { this(1); } X(int i) { this(); } }
kind regards,
JosLast edited by JosAH; 07-27-2010 at 01:07 PM.
Similar Threads
-
cannot find symbol constructor
By daud in forum New To JavaReplies: 9Last Post: 08-13-2009, 03:53 AM -
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10-18-2008, 08:26 PM -
cannot find symbol symbol : class Item location: package platypos.services.order
By officialhopsof in forum New To JavaReplies: 3Last Post: 05-01-2008, 08:30 AM -
"Cannont find symbol Constructor" error
By Welsh in forum New To JavaReplies: 7Last Post: 01-25-2008, 12:12 AM -
Error: cannot find symbol constructor
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 08:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks