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

    Question How to create an interface for the whole of my classes?

    I want to create an interface for the whole of my classes in my program:

    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;
        }
    }
    As far as an interface define a sketch of methods a class must have, and as far as the only jointly class in the code is getPoids the interface should be:

    Java Code:
    	
    interface TheGivenName {
       public Class getPoids();
    }

    There are two interfaces that implements the other things in both classes, should it affect the interface I just created?

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

    Default Re: How to create an interface for the whole of my classes?

    Interfaces are valid for all classes ... if the class implements it.

    Right now none of your classes implement the interface TheGivenName.
    The class Envoi implements the interface Expediable.
    The class Carton implements the interface CartonExpediable.

    The interfaces Expediable and CartonExpediable do not seem to contain any methods, otherwise they would have to be implemented in your class (I hear the screaming of all Java 8 pro's: let's try to ignore default methods here, for now).

    The interface TheGivenName defines the method getPoids(). Are you sure the return type is correct?
    Last edited by benji2505; 12-03-2015 at 04:54 AM.

  3. #3
    Join Date
    Nov 2015
    Posts
    17
    Rep Power
    0

    Default Re: How to create an interface for the whole of my classes?

    Okay should I implements

    Java Code:
    	
    interface TheGivenName {
       public Class getPoids();
    }
    in both of my classes?

    Or should I create an interface with the name or the two other interfaces to create an interface for the whole of my classes?

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

    Default Re: How to create an interface for the whole of my classes?

    It depends on what you want to do. If you want all affected classes to implement the same methods you can put them in one interface. If some methods in these interfaces only have to show up in some classes you can create multiple interfaces.

    Classes can implement multiple interfaces, separate them with a ",".

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to create an interface for the whole of my classes?

    If your classes have no method(s) in common, your interface can only be empty, i.e. it's a so called 'marker interface'; more important is that designing an interface as a afterthought is the wrong way to go ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 6
    Last Post: 11-30-2014, 03:53 AM
  2. Replies: 1
    Last Post: 07-08-2012, 06:23 PM
  3. interface vs abstract classes
    By chandrasekhar.melam in forum New To Java
    Replies: 1
    Last Post: 10-05-2011, 03:46 PM
  4. Create extra GUI interface
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 03-27-2011, 11:05 AM
  5. interface vs abstract classes
    By rosh72851 in forum New To Java
    Replies: 7
    Last Post: 11-16-2008, 09:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •