6 Replies Latest reply on Jan 16, 2008 10:30 AM by 807603

    Abstract class

    807603
      Hi,
      can u tell me the exact difference between abstract class and interface.under which situations we should use this.
        • 1. Re: Abstract class
          807603
          Hmm. Nobody's asked this before, we'll need some time to think
          • 2. Re: Abstract class
            800322
            Show me how you provide method implementations using interfaces.
            • 3. Re: Abstract class
              807603
              Anumanick wrote:
              Hi,
              can u tell me the exact difference between abstract class and interface.under which situations we should use this.
              A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class. Additionally, a class extending an abstract class can implement an infinite number of it's own methods.

              An abstract class can contain non-abstract methods which do not have to be overridden in the subclass.

              Edited by: AmitChalwade123456 on Jan 16, 2008 10:20 AM
              • 4. Re: Abstract class
                800322
                AmitChalwade123456 wrote:
                Additionally, a class extending an abstract class can implement an infinite number of it's own methods.
                ??? A class implementing an interface can implement its own methods as well. And it's not even theoretically "infinitly many", the bytecode specs place a hard limit on that (unsigned 2 bytes IIRC).


                Oh, and who says it's "either or"? You can use both.
                • 5. Re: Abstract class
                  807603
                  CeciNEstPasUnProgrammeur wrote:
                  AmitChalwade123456 wrote:
                  Additionally, a class extending an abstract class can implement an infinite number of it's own methods.
                  ??? A class implementing an interface can implement its own methods as well. And it's not even theoretically "infinitly many", the bytecode specs place a hard limit on that (unsigned 2 bytes IIRC).


                  Oh, and who says it's "either or"? You can use both.
                  Agreed!!
                  • 6. Re: Abstract class
                    807603
                    AmitChalwade123456 wrote:
                    Anumanick wrote:
                    Hi,
                    can u tell me the exact difference between abstract class and interface.under which situations we should use this.
                    A class implementing an interface must implement all of the methods defined in the interface
                    public abstract class ImNotImplementingThat implements Runnable {
                    
                      // public void run() {} I was going to implement this, but I can't be bothered
                    
                    }
                    while a class extending an abstract class need not implement any of the methods defined in the abstract class.
                    public class WhyAreYouMoaningAboutRead extends java.io.InputStream {
                    
                       // I won't compile, because I'm not implementing the abstract void method 'read' 
                    
                    }
                    Additionally, a class extending an abstract class can implement an infinite number of it's own methods.
                    Erm
                    An abstract class can contain non-abstract methods which do not have to be overridden in the subclass.
                    Indeed