The moose likes Beginning Java and the fly likes interface vs abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface vs abstract" Watch "interface vs abstract" New topic

interface vs abstract

rameshbabu sreebs

Joined: Oct 10, 2005
Posts: 2
i have face same questions in different interview like what is the difference between abstract and interface

pls tell me ? in java design point of view
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Please post in the Java forum.Thanks

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Moving to "Java in General (Beginner)."

[Jess in Action][AskingGoodQuestions]
Rob Spoor

Joined: Oct 27, 2005
Posts: 20192

1) a class can implement only one class (abstract or not), but implement multiple interfaces
2) an abstract class can provide partial or even full implementation, whereas interfaces have no implementation at all (every method is implicitly public abstract and every field is implicitly public static final)

How To Ask Questions How To Answer Questions
Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 661

Well the very basic difference is all methods in an interface are abstract but in that of an abstract class it isn't.

but that's just very basic.
Try this page

If you are not laughing at yourself, then you just didn't get the joke.
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
I think of an abstract class as something an object can be. So you can extend and abstract class, as an example I would use a car, you have an abstract class Car, so a SportCar is an extension of a Car cause it can do the same things a Car can do, only that it could do them in a different fashion. An interface is a bridge, is some kind of specification you must comply in order to interact with a system. So you have a I/O interface to interact with your computer, and it can be a mouse or a keyboard, and interface is a kind of standard to help two systems interoperate, as a language, language is the interface which helps two persons to communicate.

She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

An abstract class is any class that is declared as "abstract"; this means that you cannot create an instance of that class, you must create an instance of a (concrete) subclass. Typically, although not all the time, an abstract class will have abstract methods--that is, methods that have a declaration but no implementation. These methods must be implemented in a subclass, or else that subclass must be declared as abstract as well and the methods must be implemented in some subclass of the subclass.

An interface is (at a basic level) an abstract class with no methods defined. In addition, it can only define member variables that are constant (final).

You would use an abstract class when you wanted to provide base functionality but require some specific details in some methods. An example would be a Vertebratum class. This class ensures that all animals in the class have a backbone, but you cannot actually instantiate a Vertebratum object. You would have to instantiate a HomoSapiens (Human) or a CanisDomesticus (Dog) subclass.

use an interface when you think that a large number of different things can perform similar functionality. An example would be a Bird and a Bat class. Bird is a subclass of Vertebratum, while Bat is a subclass of Mammalia (which is a subclass of Vertebratum). However, both Birds and Bats can fly, so they both implement the Flyer interface (along with a number of Insects)

(Note that in this example, Bird and Bat would both be abstract classes; also, the Penguin subclass of bird would throw some sort of exception (most likely java.lang.UnsupportedOperationException) when you asked it to fly, although it would implement the Swimmer interface)

Hope that this helps.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: interface vs abstract
It's not a secret anymore!