my dog learned polymorphism
The moose likes Beginning Java and the fly likes Abstract and Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract and Interface" Watch "Abstract and Interface" New topic

Abstract and Interface

Uzma Khan

Joined: Nov 01, 2001
Posts: 1
Plz. Could anybody tell me about the difference between Abstract and Interface Class and also i want to know what is the use of Abstract class?
shweta agarwal

Joined: Aug 30, 2001
Posts: 10
abstract classes r used when u want to make sure that their is a method that the subclass has to inherit and it does not a means to provide multiple inheritence but interfaces r used for multiple inheritence.
also u can make a reference of interface but not of any abstract class.
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
Actually, the only real differences I see between abstract classes and interfaces are A) you have to implement an interface, where as an abstract class is extended and B) an abstract class can contain methods with implementations, where as an interface can not and C) you can only extend one class, but you can implement any number of interfaces. While interfaces are Java's answer to multiple inheritance, it does not function quite the same way.
I can have the following code:
And I'll get "Foo!" printed out. I get the exact same response if I have this code:
The two abstract and interface classes act the same way here. However, I could add another method to the abstract Foo class, and give it an implementation, but could not do the same to the interface Foo, the compiler would give an error. Preferably, I would use the interface version over the abstract, since you only get to extend one class. The only time you would want to use abstract (correct me if I am wrong here) classes is if that class also needed some of its own implementation, or just to make the design make sense (an abstract Animal class probably wouldn't be a logical interface).
Hope that helps
[This message has been edited by jason adam (edited November 01, 2001).]
I agree. Here's the link:
subject: Abstract and Interface
It's not a secret anymore!