Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Abstract Vs. 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 Vs. Interface" Watch "Abstract Vs. Interface" New topic
Author

Abstract Vs. Interface

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
what is difference between Abstract and Interface?
Thanks
Angela
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
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


Rob
SCJP 1.4
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
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!


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Abstract Vs. Interface
 
Similar Threads
implements a listener
abstract class Vs interface
abstract interface
Interface problems...
i want answer