5 Replies Latest reply on Mar 9, 2003 2:36 PM by 807541

    Difference % Abstract class and Interface?

    807541
      Hi,

      I am confused about abstract class and interface. For interface, the subclass has to implement all the parent class methods. But, it is no need in abstract class.

      However, in which situation should I use abstract class or interface?

      Thanks for explanation.

        • 1. Re: Difference % Abstract class and Interface?
          807541
          An interface is not a class, it's just a list of methods. When a class implements an interface it provides/defines all the methods in that list. A class can implement many interfaces.

          An abstract class is a class where not all methods are defined (they're just listed). When a class extends an abstract class it must provide/define all listed methods of the abstract class. If it doesn't it must be declared abstract itself. Only concrete (non-abstract) classes can be instantiated to objects.
          • 2. Re: Difference % Abstract class and Interface?
            807541
            http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
            http://java.sun.com/docs/books/tutorial/java/interpack/interfaceDef.html
            • 3. Re: Difference % Abstract class and Interface?
              807541
              In an interface you are not allowed to provide any functionality to your methods, which you can in an abstract class. You would use them in a case where you have a method that does something, lets say connects to a database for example, and then you have other methods that subclasses need to handle on their own. For this you would create and abstract class. Here is an example.
              abstract class Query
              {
                abstract void filterData(Resultset results);
              
                public Query()
                {
                    connect();
                    filterData(doQuery());
                }
                public void connect()
                {
                   // do connection stuff
                }
                public Resultset doQuery()
                {
                    // do query
                }
              }
              
              class OrderedQuery extends Query
              {
                public void filterData(Resultset r)
                {
                   // do sorting
                }
              }
              class SomeOtherFilterQuery extends Query
              {
                public void filterData(Resultset r)
                {
                   // do some sort of filtering on the results
                }
              }
              Basically you extend an abstact class and implement an Interface. In reality an Interface is an abstract class itself, specifying interface tells the compiler that you don't plan to provide any implemnetation for the methods, and even though you don't have to use the keyword abstract in front of your interface methods, the compiler puts it there along with public.
              interface SomeInterface
              {
                  void doSomething();
                  // these two lines are identical during compilation
                  public abstract void doSomething();
              }
              • 4. Re: Difference % Abstract class and Interface?
                807541
                Then, when should I use interface and when should I use abstract class in designing program?

                Interface seems mainly for overloading.
                • 5. Re: Difference % Abstract class and Interface?
                  807541
                  Thanks JustLee,

                  I got it now.