Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Question Does this code as internal classes? How to create one?

    Good evening!

    I'm now learning what internal classes and interfaces are. I have some difficulties understanding what are the first ones.
    I'm wondering what is the organization of the class of this code, that is to say:

    1. Its hierachy,
    2. its class variables and instance variables
    3. its methods


    Java Code:
    public class TestCI { // To modify
        public static void main(String[] args) {
            Expediable c1 = new Carton(10);
            Expediable c2 = new Carton(20);
            Expediable c3 = new Carton(25);
            Expediable c4 = new Carton(190);
            System.out.println("liste envois "+Envoi.ListeEnvois);
        }
    }
    class Envoi extends LinkedList implements Expediable{
        static List ListeEnvois = new LinkedList();
        Envoi(){
            super();
        }
        Envoi(Colis c){
            this();
            add(c);
            ListeEnvois.add(this);
        }
        public int getPoids(){ 
            int r=0;
            for(int i=0;i<this.size();i++)
            r+=((Envoi.Colis)get(i)).getPoids();
            return r;
        }
    }
    class Carton implements CartonExpediable{
        int poids;
        int no;
        static int noC=1000;
        String dest;
        public int getPoids(){
        return poids;
    }
        Carton(intpoids){
    // if we can't add it it to a Colis then creation of a new Colis
            no=++noC;
            this.poids = poids;
        }
        public String toString(){
        return"Carton n "+no+" poids: "+ poids;
        }
    }
    1. I said that Envoi and Contenu were mother class.
    2. that the variables of class were
      • in Envoi: r
      • in Contenu poids & noC


      and variables of instance were:
      • in Envoi Colis: c
      • in Carton: poids

    3. and that the method were:
      • In Envoi: getPoids
      • In Carton: getPoids & toString



    Yet, I'm not sure of what variables of instance and variables of class are, empirically in this code... Furthermore, if I found mother class, isn't it odd not founding children classes? especially when learning internal classes?

    Furthermore, I want to specify the constructors to respect this nesting: cartons in envoi in colis. but I'm not sure if I have to replace only the constructor of the second class or to nest the whole second class in the first one and creat an actual internal class... (but that seems to be heavy changes? Or is it the best way to show up what an internal class is?)

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Does this code as internal classes? How to create one?

    I have a feeling that you are confusing inner classes (officially called "nested class") and class inheritance (super-class and sub-class). In the code sample I do not see either one, except the class Envoi that inherits from a native Java class (LinkedList). Check this.

    Is it correct to assume that ListeEnvoi shall be a LinkedList of the instances of Envoi? Why do you put the instantiation inside this class then?
    Last edited by benji2505; 12-03-2015 at 03:11 AM.

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Does this code as internal classes? How to create one?

    After you have straightened out the hierarchy in your classes, the variable types will be (almost) self explanatory. As a refresher I would suggest this.

Similar Threads

  1. Replies: 0
    Last Post: 05-19-2013, 04:49 AM
  2. Replies: 4
    Last Post: 04-30-2012, 09:04 PM
  3. Create user-defined Classes & Methods
    By lala in forum New To Java
    Replies: 6
    Last Post: 02-02-2011, 02:03 PM
  4. Replies: 1
    Last Post: 07-29-2008, 03:08 AM
  5. How to create your own dialog classes in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 07:54 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
  •