Results 1 to 6 of 6
  1. #1
    bflhr673 is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default 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){

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

    Default

    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.

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

    Default

    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);

  4. #4
    bflhr673 is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    just to make sure i understand....so i cant have both a super(p) and super(a,b) correct?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    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.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,538
    Blog Entries
    7
    Rep Power
    20

    Default

    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:

    Java Code:
    class X {
    	X() { this(1); }
    	X(int i) { this(); }
    }
    ... and the compiler will notice it (it's not that stupid)

    kind regards,

    Jos
    Last edited by JosAH; 07-27-2010 at 01:07 PM.

Similar Threads

  1. cannot find symbol constructor
    By daud in forum New To Java
    Replies: 9
    Last Post: 08-13-2009, 03:53 AM
  2. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  3. Replies: 3
    Last Post: 05-01-2008, 08:30 AM
  4. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 12:12 AM
  5. Error: cannot find symbol constructor
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 08:24 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
  •