Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 02-03-2009
Member
 
Join Date: Jan 2009
Posts: 10
What's the difference between an interface and an abstract class in JAVA?

Hi,

What's the difference between an interface and an abstract class in Java?
Reply With Quote
  #2  
Old 02-03-2009
Member
 
Join Date: Jan 2009
Posts: 9
Re: What's the difference between an interface and an abstract class in JAVA?

An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
Reply With Quote
  #3  
Old 02-03-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: What's the difference between an interface and an abstract class in JAVA?

An Abstract Class can contain default Implementation, where as an Interface should not contain any implementation at all. An Interface should contain only definitions but no implementation. where as an abstract class can contain abstract and non-abstract methods. When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class. an abstract class can inherit from another non-abstract class.
Reply With Quote
  #4  
Old 02-03-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: What's the difference between an interface and an abstract class in JAVA?

1. Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2. Variables declared in a Java interface is by default final. A Java abstract class may contain non-final variables.
3. Memebers of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
4. Java interface should be implemented using keyword ?implements?; A Java abstract class should be extended using keyword ?extends?.
5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
6. A Java class can implement multiple interfaces but it can extend only one abstract class.
7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.
Reply With Quote
  #5  
Old 02-03-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: What's the difference between an interface and an abstract class in JAVA?

Neither Interface or Abstract classes can be instantiated. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.

Use interface as much as possible and only use an abstract class in the case where you want to provide some (but not all, of course) implementation. In practice, you want to prefer using, passing and returning interfaces, not abstract class references. I think even in the case where you have an abstract class to share some implementation, you still want to have an interface represent what will be used by the client (for an example look at the new Collections -- you often pass around Collection, List, Set or Map references, but not a AbstractList, AbstractSet or AbstractMap, references)
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What's the difference between an interface and an abstract class in JAVA?"
Thread Thread Starter Forum Replies Last Post
Problem with abstract class and interface Vodka Software Development 5 08-03-2010 12:20 PM
Can I extend a java class or interface in a JSP page? Juaquine Software Development 4 17-02-2010 09:07 PM
Difference among concrete class and abstract class Roxy_jacob Software Development 4 07-12-2009 01:22 PM
Abstract class in Java SoftWore Software Development 3 07-11-2009 01:58 PM
Abstract class and Interface in .net RogerFielden Software Development 3 04-05-2009 06:07 PM


All times are GMT +5.5. The time now is 09:30 AM.