abstract class structure - cannot find symbol error

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: abstract class structure - cannot find symbol error

Hybrid View

  1. #1
    Join Date
    Jul 2005

    abstract class structure - cannot find symbol error

    This is for an assignment. (Cross post - http://www.codeguru.com/forum/showthread.php?t=384691)

    I have to keep track of different produce types. To do this, I have created a class structure as follows:

    abstract class Produce
    abstract class Crop extends Produce
    abstract class Livestock extends Produce
    class Wheat extends Crop
    class Apple extends Crop
    class Cow extends Livestock
    class Sheep extends Livestock
    The code was written in this fashion to allow for other produce types to be added into the system as required.

    Produce creates abstract methods that are common to all produce types (eg getSaleValue(), getMonthlyCosts()) as well as concrete methods (eg getProduceName())

    Crop and Livestock introduce abstract methods and concrete methods that are particular to crops and livestock (eg Crop has getMaturity()).

    Wheat - Sheep define all of the classes necessary.



    Due to the necessay generic nature of the produce types (ie land can have Produce), all new produce is created as Produce p = new Wheat() (or new Cow() etc...).

    Now I'm running into the problem that I can't access the methods introduced in Crop and Livestock, as the produce is all of type Produce, and the methods don't exist in there. These are compilation errors.

    I've probably done something very stupid, and just can't recall something from one of my first lectures (or I'm not looking up the correct key words), but how can I access (for instance) getMaturity() from object p, when I create p as Produce p = new Wheat()

    Sample code and error:

    Produce a = new Wheat();
    Produce b = new Cow();
    // add some more produce related constructors here
    //p has been defined previously as a type of produce as in the example above
    if(p instanceof Crop) 
    E:\test classes\ProduceTester.java:31: cannot find symbol
    symbol : method setMaturity(int)
    location: class Produce

    My question repeated:
    How can I access methods defined in a child class when the object is of the parent class?
    Last edited by masher; 04-23-2006 at 10:09 PM.

  2. #2
    Join Date
    Feb 2006
    Cologne - Germany
    try casting it:
    if(p instanceof Crop) {
    Crop c = (Crop) p;

    or shorter:
    if (p instanceof Crop) {

  3. #3
    Join Date
    Jul 2005
    Problem solvered. See codeguru site.

  4. #4
    Join Date
    Apr 2006

    no define variant

    no define variant

Similar Threads

  1. Replies: 1
    Last Post: 04-13-2006, 04:57 AM
  2. what is error with this class ?
    By MaaSTaaR in forum Java
    Replies: 3
    Last Post: 12-15-2005, 05:21 PM
  3. Replies: 1
    Last Post: 09-25-2005, 10:09 AM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Replies: 0
    Last Post: 12-08-2000, 07:00 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
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center