File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes abstract class 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 class and interface" Watch "abstract class and interface" New topic

abstract class and interface

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
When do we use an abstract class and when do we use an interface?
El Roy Cabildo

Joined: Oct 19, 2002
Posts: 23
interface is a collection of methods..view it as a template which those methods should be implemented when sub classed..while abstract is a partial class means their is a method that don't have implementation..means subclassing a abstract class should implement its abstract method otherwise declare the child as abstract...just like for example...
A class Human...not all human do smoke() so you make it as an abstract so...if you create a class named Amit and subclass the Human class and amit do Smoke() you implement that you do smoke.
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
that means when we want only some methods to be implemented then we use abstract class otherwise we use interface.
The decision of whether to create an abstract class or an interface depends on the design. right??
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi amit,
yes. it depends on how u think and what u have in front of u.
in one application something can be an interface but in the other application we would love to create an abstract class know...
also, in creating abstract class the risk is that the implementing class can't extend other class. so we might be in trouble due to this.
though i've not come across such a thing so far.
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231
Brian Burke

Joined: Oct 26, 2000
Posts: 15
another distinction that maulin touched on is that an interface is implemented and an abstract class is sublassed. For example. If you have a Human interface (to steal the example from above) you would say.
public class DoSomething implements Human{ }
Whereas if Human is an abstract class, it would look like.
public class DoSomething extends Human{ }
You can implement multiple interfaces, but can only subclass one class.
I agree. Here's the link:
subject: abstract class and interface
It's not a secret anymore!