6 Replies Latest reply: Jan 27, 2009 9:39 PM by DarrylBurke RSS

    difference between interface and  abstract class

    807597
      hai

      please tell me what is the difference between interface and abstract class?

      when i want use inteface? when i want use abstrct class?

      give me the simple example for that?......
        • 1. Re: difference between interface and  abstract class
          794117
          A commonly asked question. Heres one answer to it

          http://forum.java.sun.com/thread.jspa?threadID=643081&tstart=0
          • 2. Re: difference between interface and  abstract class
            807597
            hi ,

            One thing , u keep in mind , it depends on the requirements and the design u choose , that u will need interface or abstract classes.

            Take a look at this scenario :

            Requirement : we should have 2 objects Dog and Cat with some of their related features.
            Considering this , immediately it will strike in ur mind that i need to have 2 classes named " DOG" and "CAT".
            Now u will think that some of the features of the DOG & CAT can be common . ( for eg: -- say : breathe. ) .besides this , there might be other common features too...right ?
            Now .. if they have some common features, y not create an abstract class. lets say : abstract class named " PET".
            Now think in a real life scenario.."What is "PET" ? can it be something concrete ....No !! A DOG can be concrete ,CAT can be concrete , but not PET.
            now u can have a method say public void breathe(); now both conrete classes : DOG & CAT can extend from the abstract class "PET" .

            Now . this design goes well..!!.
            Lets say now the Pet Department wants to take away the dogs & cats through some defined procedures.
            Lets say thye wnat to apply the following:
            1. Check() ; //check whether dog/cat is healthy.
            2.Wash(); /// wash if dirty.

            So now u want to apply the same to the dogs and cats .
            We can do by adding these methods in the PET class.
            But think for a min..?Will this be a good object oriented design ?
            No no & No . Simply because check() and wash() does not comes as characteristic feature of the PET. It is only added by the PETDEPARTMENT.
            Now..u know what to do here... create an abstract class "PET DEPARTMENT"
            with methods :
            1. Check() ; //check whether dog is healthy.
            2.Wash(); /// wash if dirty.
            so that class DOG & CAT can extend the "PETDEPARTMENT "class and implments the methods.
            But wait a min here... is it possible for a class to extend 2 classes( A DOG/ CAT class cannot extend both classes " PETDEPARTMENT" as well as "PET" class .) No it cannot extend multiple classes.

            How to achieve multiple inheritance in JAVA?
            Yes u got it ... through " INTERFACE".

            now make "PETDEPARTMENT" as interface....

            that would provide the solution .

            I guess that helps U .
            • 3. Re: difference between interface and  abstract class
              843785
              Hi this Priya,


              Acutuallly i have a question interface and Abstract class.

              i need soultion for this particular question, how we do it.


              Create an interface called Pet. This class should have at least two methods in it. Create two classes named Cat and Dog that implement this interface. Write a program that creates a TreeSet; adds Cats to it, and outputs the values sorted by the names of the pets. Make sure that your Pet interface contains the compareTo method and your Cat and Dog classes implement the Comparable interface also.


              Please reply back me with soultion.


              thanks
              Priya
              • 4. Re: difference between interface and  abstract class
                794117
                I was just about done this, but then I added an extra method to the Dog class:
                public class Dog implements Pet{
                public void eatHomework(Object h)
                
                }
                Unfortunately during the testing of this method, the rest of the classes I wrote were lost. You see the dog started chasing the cat, and it climbed the tree set to get away. Then the dog cocked his leg up against the tree set and ... erm... interfaced with it in the way that only dogs can. Well ... you can guess what a mess things are in right now.

                Anyway I would suggest two things to you swatiprya:
                1 - Do your own homework. Figure out some basics and post what you've done here. We don't mind pointing you in the right direction if you show some effort on your part.
                2 - If you have a new question, post a new topic. Why oh why did you resurrect this topic from the graveyard? Its over 4 years since the question was asked. You're meant to be learning programming, not necromancy.

                I'm expecting somebody along soon to close this thread. :-)
                • 5. Re: difference between interface and  abstract class
                  DarrylBurke
                  swatipriya716

                  Welcome to the forum. Please don't post in old threads that are long dead. When you have a question, please start a topic of your own. Feel free to provide a link to an old thread if relevant.

                  I'm locking this thread now. It's 3 years old.

                  db
                  • 6. This Thread is now moved
                    DarrylBurke
                    This thread is moved from "JavaServer Pages (JSP) and JSTL" to here.