When do we use an abstract class and when do we use an interface?
El Roy Cabildo
Greenhorn
Joined: Oct 19, 2002
Posts: 23
posted
0
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
posted
0
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: 1865
posted
0
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 instead...you 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. regards maulin.
amit, 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. HTH, Brian