18

What is the exact use of an Abstract class? Is not possible to do the same things in an ordinary class as it is an an abstract class?

1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
  • See: http://stackoverflow.com/questions/56867/interface-vs-base-class – John Sheehan Oct 27 '08 at 05:13
  • Take a look at [Template Method Design Pattern](http://sourcemaking.com/design_patterns/template_method) I think abstract class is unreplaceable there. – rafek Oct 27 '08 at 05:22

4 Answers4

24

Use an abstract class to provide some concrete implementation but not allow instantiation. You can always instantiate an ordinary class which doesn't make sense if it can't stand alone. At the same time, an interface might not be enough if there's a concrete implementation that's identical in all implementing classes. An abstract class is just enough.

  • Interface: contract only, no implementation, no instantiation
  • Abstract class: contract, some implementation, no instantiation
  • Class: contract, implementation, instantiation
Corbin March
  • 25,526
  • 6
  • 73
  • 100
  • how it i possible contract in third one, class. because contract means declaration of that method, that means "abstract". And compiler forces to converts into abstract class Thats why how it is possible contract in normal class. If my logic is wrong, please mke me correct. – devsda Sep 16 '12 at 15:55
  • I'm confused what you mean by contract – ThomasRones Sep 30 '21 at 12:45
19

An abstract class is used when you have some base functionality that you want subclasses to inherit, but it wouldn't make sense to instantiate the base class. For example, if you had something like a Shape base class, you could have some built in implementation that could be used by subclasses as well as interface methods that you want the subclasses to implement. However, it probably wouldn't make sense to create a Shape object. An abstract class gives you this functionality. Another great example of abstract class uses is the abstract factory pattern.

AdamC
  • 16,087
  • 8
  • 51
  • 67
5

Regular classes require you to provide implementations for all methods.
Interfaces require you to not provide any implementations for all methods.

Abstract classes are the only type of class that allow you to both have methods that do contain an implementation, and have methods that do not provide an implementation, but require an inheriting class to provide one.

The fact that you are allowed to add methods without an implementation is the reason you cannot instantiate an abstract class: you can only instantiate something that has implementations for all its methods.

Robert Cartaino
  • 27,494
  • 6
  • 45
  • 67
user31768
  • 51
  • 1
1

Unlike regular classes, abstract classes can contain abstract methods. They act much like interface members.

Meanwhile, they can do just about everything else that regular classes can do: they can implement methods, contain fields & nested types, derive from another class, etc.

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168