Results 1 to 14 of 14

Thread: Nested Classes

  1. #1
    new.guy is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default Nested Classes

    I am trying to work through a problem with nested classes.

    I have a super class, Fruit, and several different sub classes called Apple, Banana, Grape, etc... The Fruit class has several different methods, and each nested class has a method called Eat(), which differ slightly from class to class.

    This is all working fine, except I have an ArrayList of fruit, which contains a collection of Apples, Bananas, Grapes, etc...

    Java Code:
    ArrayList<Fruit> myFruit = new ArrayList<Fruit>();
    Now what I want to do is call the Eat() method directly from the ArrayList, by doing something like
    Java Code:
    myFruit.get(a).Eat();
    where the Fruit in element a is an Apple.

    However, when I try to compile this, it gives me the error that it cannot find a constructor of type Fruit. So what I believe this means is that my ArrayList is a collection of Fruit, and thus even though element a is also an Apple, it can only access the Fruit methods.

    Does anyone have any ideas about getting around this?
    Thanks
    Last edited by new.guy; 09-06-2008 at 05:10 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    gives me the error that it cannot find a constructor of type Fruit.
    Please post full text of error message with source code causing it.

  3. #3
    new.guy is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    This was not the exact code I was working on, I used the Fruit example to help illustrate the problem I was having because the code I've written so far spans over a few different documents, with several hundred lines of code. I figure that posting such a chunk of code into the forum wouldn't be feasible.

    I'll try to make the issue a little clearer:

    I have several different classes all nested inside one super class, which I called Fruit for convenience. Each individual nested class has one method,
    Eat(Apple theApple), Eat(Banana theBanana), Eat(Grape theGrape)... which are all similar but all take different kinds of fruit as parameters.

    Then, in a completely different file that is a part of the same package, I have an ArrayList of Fruit, into which I have put different kinds of fruit.

    Java Code:
    ArrayList<Fruit> myFruit = new ArrayList<Fruit>();
    All of this has been working fine, up until this point:

    I want to be able to access the Eat() methods from the ArrayList, by calling

    Java Code:
    myFruit.get(a).Eat(myFruit.get(a));
    where element 'a' is an apple, for instance.
    However, when I try to compile this code I get the error that it cannot find the symbol: method Eat(Fruit.Apple).

    It seems to me that since I constructed the ArrayList myFruit to be of type Fruit, and not of the individual nested classes of fruit, it will only let me access the Fruit methods, and not the individual Eat methods.

    My basic question, therefore, would be is there a way, given an ArrayList of a super class, to access the methods of the individual nested classes?

    Hopefully this makes my question clearer.
    Thanks

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    What is a nested class? There are inner classes and extended classes. A class can have local variables that reference other classes.
    Can you write a simple example program that shows what you want to do?

    Is the question: If an apple is a fruit, then is a fruit an apple? No a fruit is not an apple.

  5. #5
    new.guy is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    Yeah, I guess nested class is just another name I use for an inner class.

    I realize that an Apple is a Fruit but a Fruit isn't an apple... what I'm trying to do is access the Apple methods through an ArrayList of Fruit. I'll try to write up a small sample program here.

    Java Code:
    class Fruit{
          String name;
          public Fruit(String name){
               this.name=name;
          }
    
          public void Buy(Fruit theFruit){
               System.out.println("You bought the fruit!");
          }
          class Apple extends Fruit{
               public Apple(){
               }
               public void Eat(Apple theApple){
                    System.out.println("You at the "+theApple.name);
               }
          }
          class Banana extends Fruit{
               public Banana(){
               }
               public void Eat(Banana theBanana){
                    System.out.println("You at the "+theBanana.name);
               }
          }
    }
    Then I have in a different document an ArrayList:
    Java Code:
    ArrayList<Fruit> myFruit = new ArrayList<Fruit>();
    I want to be able to do something like this:
    Java Code:
    myFruit.get(a).Eat(myFruit.get(a));
    The problem is that the elements of the arraylist are being considered only fruit, and not Fruit.Apple, Fruit.Banana, etc...

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

    Default

    1) why are you using inner classes here? There's a time and place for them, but this is not it.
    2) why does your eat method take a parameter? If it should output a name, it should be the this.name. There should only be one subject for this sentence.

    Java Code:
    public void Eat(){
      System.out.println("You at the " + this.name);  // this is redundant, but it's to drive  home a point that there is only one subject for this sentence.
    }

  7. #7
    new.guy is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    I am not, in fact, interested in making a program that involves fruit, I chose that as a simpler example that would allow me to demonstrate what I wanted. I'm well aware that the above fruit class example is redundant because it was just something I typed up in 2 minutes to use as an example.

    I'm using the inner classes because I wanted to be able to have an ArrayList of fruit, that would be able to store all the instances of the different "fruit" while still allowing me to have different methods for each type of fruit.

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

    Default

    I am not, in fact, interested in making a program that involves fruit, I chose that as a simpler example that would allow me to demonstrate what I wanted. I'm well aware that the above fruit class example is redundant because it was just something I typed up in 2 minutes to use as an example.
    All well and good, but if you're posting here to learn, you're going to have to expect criticism given in the spirit of learning to do things correctly. Having a parameter in the method is not s redundant, but rather it's simply wrong.

    I'm using the inner classes because I wanted to be able to have an ArrayList of fruit, that would be able to store all the instances of the different "fruit" while still allowing me to have different methods for each type of fruit.
    But inner classes don't give you this ability.

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

    Default

    To use your example:

    Java Code:
    // make it abstract as we'll never instantiate a "Fruit" object
    public abstract class Fruit
    {
      private String name;
    
      public Fruit(String name)
      {
        this.name = name;
      }
      
      public void eat()
      {
        System.out.println("You are eating fruit named " + name);
      }
    }
    Java Code:
    public class Apple extends Fruit
    {
      public Apple(String name)
      {
        super(name);
      }
      
      public void eat()
      {
        System.out.print("You are eating an apple, and ");
        super.eat();
      }
    }
    Java Code:
    public class Banana extends Fruit
    {
      public Banana(String name)
      {
        super(name);
      }
      
      public void eat()
      {
        System.out.print("You are eating a banana, and ");
        super.eat();
      }
    }
    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class FruitList
    {
      private List<Fruit> fruitList = new ArrayList<Fruit>();
      
      public FruitList()
      {
        fruitList.add(new Apple("Fred"));
        fruitList.add(new Banana("Wilma"));
        fruitList.add(new Apple("Barney"));
        fruitList.add(new Banana("Betty"));
      }
      
      public void eatAll()
      {
        for (Fruit fruit : fruitList)
        {
          fruit.eat();
        }
      }
      
      public static void main(String[] args)
      {
        FruitList fList = new FruitList();
        fList.eatAll();
      }
    }

  10. #10
    new.guy is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    So you are saying there is no way for me to go directly from the elements in my ArrayList to the eat() methods found in the inner classes?

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

    Default

    So you are saying there is no way for me to go directly from the elements in my ArrayList to the eat() methods found in the inner classes?
    There are ways but not straightforward. Inner classes in my mind are mostly used as helper classes to help the containing class perform its functions. AFAIK, they aren't supposed to be exposed to the outside world and manipulated. So, the more important question (to me) is, if you are manipulating these classes, again, why are you making them internal. I know that this is just an example program, but what big overriding question are you trying to answer here?

    I could make Fruit non-abstract and add an inner class, Pear like so:
    Java Code:
    public class Fruit
    {
      private String name;
    
      public Fruit(String name)
      {
        this.name = name;
      }
      
      public void eat()
      {
        System.out.println("You are eating fruit named " + name);
      }
      
      class Pear extends Fruit
      {
        public Pear(String name)
        {
          super(name);
        }
        
        public void eat()
        {
          System.out.print("You are eating a pear, and ");
          super.eat();
        }
      }
    }
    and then I can use this Pear class if I first create a Fruit instance on which to call Pear's constructor, like so:
    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class FruitList
    {
      private List<Fruit> fruitList = new ArrayList<Fruit>();
      
      public FruitList()
      {
        fruitList.add(new Apple("Fred"));
        fruitList.add(new Banana("Wilma"));
        fruitList.add(new Apple("Barney"));
        fruitList.add(new Banana("Betty"));
        fruitList.add(new Fruit("Fubar").new Pear("Bam Bam")); // *** here ***
      }
      
      public void eatAll()
      {
        for (Fruit fruit : fruitList)
        {
          fruit.eat();
        }
      }
      
      public static void main(String[] args)
      {
        FruitList fList = new FruitList();
        fList.eatAll();
      }
    }
    But again, why bother with all this mess when it's obviously a kludge?

  12. #12
    new.guy is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    I'm using these internal classes because I needed a way to store all the information of the different fruit classes all in one place (the ArrayList), while having separate methods for each type of fruit. The only way I saw to do this was to encompass all the different kinds of fruit into one big group, and then make the ArrayList of type fruit.

    There may be better ways of doing this, but this was the only way with which I am somewhat familiar with.

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

    Default

    I'm using these internal classes because I needed a way to store all the information of the different fruit classes all in one place (the ArrayList), while having separate methods for each type of fruit. The only way I saw to do this was to encompass all the different kinds of fruit into one big group, and then make the ArrayList of type fruit.
    Sorry, but this makes no sense. Again let me try to make it as plain as possible: Nested classes don't give you this ability. Did you see my code without nested classes? The ArrayList worked fine (and in fact everything was much clearer) without nested classes.

    After being told this several times here as well as after havign been given examples, I have to ask you why you insist that you need nested classes for this functionality? Are you confusing inheritance with being "nested" because somewhere you're holding on to some basic misconception which you just have to let go of.

    Much luck with it.
    Last edited by Fubarable; 09-07-2008 at 02:47 AM.

  14. #14
    new.guy is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    Aha! It works now, thank you. I redid my entire code to use the abstract class instead of nested classes, and it works great now.

    Thanks a ton!

Similar Threads

  1. Nested(Inner) Classes
    By Z.S.Tehrani in forum New To Java
    Replies: 7
    Last Post: 08-13-2008, 10:54 AM
  2. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  3. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  4. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM
  5. Nested Tags JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2007, 05:42 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
  •