This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Abstract class and Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract class and Interface" Watch "Abstract class and Interface" New topic
Author

Abstract class and Interface

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

What is the difference between an Abstract class and Interface?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13863
    
  10

See: http://faq.javaranch.com/java/InterfaceVsAbstractClass

This is a very frequently asked question. If you want to find more information, please search the forums here or search with Google, and you'll find a lot more information.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36452
    
  15
I trust you have looked in books and the Java Tutorial?

An abstract class is one which you do not mean to instantiate; it can have abstract methods, implemented methods, fields, constructors, etc.

Imagine interfaces as being very very abstract classes.
An interface (ideally) only has methods. All the methods are empty, so you only write a list of method headings. You usually miss out "public" and "abstract." An interface might look like thisYou can't compile that because it has "java.lang" in. If you add lots of documentation comments you will reconstruct the Comparable interface, at least before Java 5 when it was Comparable rather than Comparable<T>.

When your class (unless it is an abstract class) implements an interface it also has to implement all its methods, so as to maintain the contract in the original documentation.

You can instantiate an abstract class several ways, but the one you are probably familiar is to write extends. You can implement several interfaces, so using interfaces has several advantages:
  • You can implement certain functionality from the interfaces.
  • A class using the objects can check whether a particular method is available by using "instanceof Comparable" or similar. Much quicker than looking for the method.
  • I am sure other people will have more points to add.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Abstract class and Interface
     
    Similar Threads
    Java interface
    forcing implementing methods
    abstract class Vs interface
    abstract class
    Interface problems...