File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Vs. Interface

 
Angela D'souza
Ranch Hand
Posts: 469
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is difference between Abstract and Interface?
Thanks
Angela
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is a data type, like a class. Abstract is a keyword used in declaring classes or methods. It means that the class being declared is not intended to be instantiated, but rather serve as a superclass for derived classes that can be instantiated.
All interfaces are implicitly abstract, so you don't need to specifically declare an interface abstract.
If you make a class abstract, you can't create an instance of it.
For example,
abstract class foo{

}
foo myFoo = new foo(); //will cause a compiler erorr.
You can subclass foo, howerver, and if the derived class is not abstract, you can instantiate that subclass.
class bar extends foo{

}
bar myBar = new bar(); //ok
Also, because of the way polymorphism works, you can actually use a reference to an abstract class to hold a concrete instance...

foo aFoo = new bar(); //textbook polymorphism.
You can also declare methods to be abstract. When you do this, the class MUST also be declared abstract. Abstrace methods don't have an implementation; instead of curly braces ({}) the class body is replaced by a semicolon.
abstract class foo{
abstract void aMethod(); //no body
}
Rob
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela, what textbooks are you learning with? Have you checked out the Java tutorial at Sun's site? It has some good explanation of all these questions you are asking. Actually, any good text should explain them in detail.
Here are a number of books you may want to check out.
Beginning Java 2 by Ivor Horton
Thinking in Java by Bruce Eckel.
Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic