Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class and interface

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When do we use an abstract class and when do we use an interface?
 
El Roy Cabildo
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Brian Burke
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic