13 Replies Latest reply on Dec 27, 2005 5:45 PM by 796440

    Different between Abstract  class and interface

    807590
      any one can please tell me Difference between abstract class and interface. Which one is best ?
        • 1. Re: Different between Abstract  class and interface
          807590
          Which is best, a hammer or a screwdriver?

          Google or use the forum search. This question is asked a lot. I'm pretty sure it came up at least twice in the past week.
          • 2. Re: Different between Abstract  class and interface
            807590
            http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html
            http://www.javaworld.com/javaworld/javaqa/2001-08/03-qa-0831-interface.html
            http://mindprod.com/jgloss/interfacevsabstract.html
            • 3. Re: Different between Abstract  class and interface
              807590
              thank you Very much
              • 4. Re: Different between Abstract  class and interface
                807590
                any one can please tell me Difference between
                abstract class and interface.
                From a typing perspecitive there's no difference (you can declare variables of both). The only difference in principle is that an abstract class also can have concrete methods so it allows you ti inherit implementation along with the type. You can only inherit from one class but from as many interfaces you whish.
                Which one is best ?
                The short answer is that interface is best.
                • 5. Re: Different between Abstract  class and interface
                  807590
                  Which one is best ?
                  The short answer is that interface is best.
                  I would clarify that to a not-quite-as-short answer that which one is best depends on the situation, but there are more situations in which interface is best than abstract class. :-)
                  • 6. Re: Different between Abstract  class and interface
                    807590
                    In Design Perspective Which one is Best interface or abstract class
                    • 7. Re: Different between Abstract  class and interface
                      807590
                      It depends.

                      If you understand the differences between them then you'll be able to use those differences to figure out which one is better in a given situation.
                      • 9. Re: Different between Abstract  class and interface
                        807590
                        In Design Perspective Which one is Best interface or
                        abstract class
                        Basically you should NOT compare abstract class with interface. You should compare class with interface. The class may be abstract to a varying degree. An interface is always totally abstract. It's a pure type. It can carry no implementation. The only difference between inheriting from a class and an interface is that a class may carry implementation whereas an interface cannot (and that you can inherit from one class only but from multiple interfaces).

                        So the somewhat expanded simple answer is that whether you inherit from a class or an interface doesn't matter from a typing perspective, but if you want to inherit implementation you have no choise. It got to be a class.
                        • 10. Re: Different between Abstract  class and interface
                          807590
                          In my opinion all three (Interfaces, Abstract and Classes) go hand in hand. You use them according to whether you need them or not. This statement sounds stupid but sometimes we forget that. Let us imagine a Car. The interface of a Car would be as follows:
                          interface ICar{
                          
                               public void move();
                          
                               public void changeGear(int gear);
                          
                               public void setPaint(Color c);
                          
                               public Color getPaint();
                          
                               public void addPerson(Object p);
                          
                               public void removePerson(Object p);
                          }
                          Now What if I have 20 different cars. BMV, Vauxhall, Porches, Ferrari, Lamborghini, Lotus, Jaguar, etc. We can definitely say that each one of these cars have a different way how they change their gears. Some of them may have an automatic gear box while the others manual. Some of them might have a maximum of 4 gears and others of 6 gears. Some cars can have a maximum of 5 people others of 6 or 7. So the methods changeGear, adperson / removePerson can be said to be class dependent, i.e. by this I mean that each car has it�s own individual way how to implement this. However what about the main of the car? We do not need to complicate stuff to determine the colour of the car. Thus the getPaint / setPaint can be said to be the same implementation for all cars. Even the move method is the same, since all cars will move using the same calculation.

                          Now we have to options we can either have one interface that defines the basic methods, and several classes that implement these methods for each single car. Or else we can (in the middle) create an abstract class that defines the common methods getPaint / setPaint and move. And then make the different classes extend this abstract class and thus we will have some methods already defined, others that are not which are in turn declared as abstract.

                          So our abstract class should be something like this:
                          abstract class AbstractCar implements ICar{
                          
                               private Color c;
                          
                               public void move(){
                                    // Do some pyshics logic here.
                               }
                          
                               abstract public void changeGear(int gear);
                          
                               public void setPaint(Color c){
                                    this.c = c;
                               }
                          
                               public Color getPaint(){
                                    return this.c;
                               }
                          
                               abstract public void addPerson(Object p);
                          
                               abstract public void removePerson(Object p);
                          }
                          Now with my common elements implemented I can start developing my classes, that are BMV, Jaguar, etc The class for Car would be something as follows:
                          public class Car extends AbstractCar{
                          
                               public void changeGear(int gear){
                                    // determine how a gear has to be changed.
                                    // What the int value means, example in an automatic
                                    // car 1 = drive, 0 = off, and -1 = revers (example);
                               }
                          
                               public void addPerson(Object p){
                                    // determine how to add a person.  Where to place
                                    // him, check age, see if he can drive, etc.
                               }
                          
                               public void removePerson(Object p){
                                    // The same as the other two methods.
                               }
                          }
                          Thus the conclusion is that interfaces define the type of the class that you are going to develop. Abstract classes can implement some of the common functionalities, and classes then can continue the class to it�s full state. Ps, the example above may be silly :$

                          Regards,
                          Sim085
                          • 12. Re: Different between Abstract  class and interface
                            807590
                            Hi,please kindly help me
                            • 13. Re: Different between Abstract  class and interface
                              796440
                              Hi,please kindly help me
                              Well, maybe if you'd post an actual question...