• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

abstract class and interface

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When do we use an abstract class and when do we use an interface?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic