Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface vs abstract

 
rameshbabu sreebs
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post in the Java forum.Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to "Java in General (Beginner)."
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Arun Kumarr
Ranch Hand
Posts: 661
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jaime M. Tovar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic