difference between abstract class and interface

what is the difference between abstract class and interface?
where to user abstract class and where to use interface, give real time example.?
Asheesh_KumarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gyanendra SinghArchitectCommented:
1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. A Java Interface can contain only method declarations and public static final constants and doesn't contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present.

2. Abstract class definition begins with the keyword "abstract" keyword followed by Class definition. An Interface definition begins with the keyword "interface".

3. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses

4. All variables in an Interface are by default - public static final while an abstract class can have instance variables.

5. An interface is also used in situations when a class needs to extend an other class apart from the abstract class. In such situations its not possible to have multiple inheritance of classes. An interface on the other hand can be used when it is required to implement one or more interfaces. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritance.

6. An Interface can only have public members whereas an abstract class can contain private as well as protected members.

7. 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.

8. The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass

9. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast

10.Interfaces are often used to describe the peripheral abilities of a class, and not its central identity, E.g. an Automobile class might
implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.

Note: There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.

Note: If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they
share is a set of method signatures, then tend towards an interface.

Similarities:
Neither Abstract classes nor Interface can be instantiated.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
You may find this helpful, but will give some examples of usage below:
http://java.sun.com/docs/books/tutorial/java/concepts/

Interfaces lay the framework for what a Class's public capabilities are.  These are usually named by verbs (whereas Classes are usually nouns).  For example, an interface for a class called Animal could be Speaks with a public interface of speak().

public interface Speaks {
    public void speak();
}

class Animal implements Speaks {
     private String sound;

     public void speak() {
         System.out.println(sound);
    }
}

As you can see the Animal must implement the public interface method speak() and thus outside world can trust that if they instantiate an object of any type that implements Speak interface that they can then call speak method and know that it will be there.

Speaks thing = new Animal();
thing.speak();

As you can see if allows for good object oriented programming practice as well as ensuring that in future updates you don't accidentally remove something that is being used by the outside world.

Interfaces cannot have any implementation, so Abstract classes are like methods in that they define methods that must be implemented by classes that inherit from it except you can have some concrete base code already defined to save the leg work later.

Now in this same example if we made Animal abstract, we could have a class Cat and a class Dog that both inherit from Animal but implement it in different ways as abstract classes just provide implementation framework.

abstract class Animal implements Speaks {
     protected String sound;

     public void speak() {
         System.out.println(sound);
     }
     
     void setSound(String s);
}

class Cat extends Animal {
     void setSound(String s) {
         sound = s;
     }
}

class Dog extends Animal {
     void setSound(String s) {
         sound = s + " " + s;
     }
}

Animal cat = new Cat();
cat.setSound("meow");
Animal dog = new Dog();
dog.setSound("woof");

// this is not necessary, just showing that this is still valid
Speaks speaker = cat;
speaker.speak();

Hopefully those help explain it.
0
Kevin CrossChief Technology OfficerCommented:
Took so long to type, I see BondinASP layed this out nicely above.  To follow-up on what was said, as you can see you can have an Abstract base class that implements an Interface so that you can write implementation code once and it is now usable by all the subclasses as BondinASP indicated.  As you will notice, it also defined additional methods that are not required to be public that must be implemented by any base clients that are not themselves Abstract.

Good luck!
0
Asheesh_KumarAuthor Commented:
nice answers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.