0

According to OOP, Abstract Classes are needed to model those objects that have no existence in the real-world but serves as a base class for several real-world objects.

For Example:

   BankAccount            
       /\
      /  \
     /    \
    /      \
Current     Savings
Account     Account

Here BankAccount should be modeled as an Abstract Class.

But what is the technical reason for using Abstract Classes in C#/Java?link text

For example:

The OOP reason for using Interfaces is to model Behavioral inheritance (Inheritance with no real hierarchical relationship).

The technical reason for using Interfaces in C#/Java is to solve the problem of multiple inheritance (If I am not wrong!).

Community
  • 1
  • 1
user366312
  • 16,949
  • 65
  • 235
  • 452
  • 1
    See http://stackoverflow.com/questions/417590/abstract-classes-and-methods-in-java-inheritance and http://stackoverflow.com/questions/239127/exact-use-of-abstract-class – jason Jul 27 '09 at 01:48
  • And... http://stackoverflow.com/questions/56867/interface-vs-base-class – jason Jul 27 '09 at 03:27

5 Answers5

8

Abstract classes can have default behavior if something sensible is possible; interfaces cannot.

An abstract class can provide default behavior for ALL methods or no methods; developer's choice; interfaces cannot.

Abstract classes can have state that's shared with all subclasses; interfaces don't specify state.

So your abstract BankAccount can have a balance attribute that Savings and Checking can be granted access.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I feel like it's worth mentioning abstract methods by name in any explanation of abstract methods. They allow the base class's implementation to depend on the presence of a method without having to specify the abstract method's implementation. You might call abstract classes naval-gazing interfaces. At any rate, I'm not sure why your answer wasn't accepted over that barely-literate one-liner. – Isabelle Wedin Jul 27 '09 at 02:43
  • Ah, "any explanation of abstract methods" should, of course, have read "any explanation of abstract classes." That's what happens when you point the "barely-literate" finger, I guess. – Isabelle Wedin Jul 27 '09 at 02:44
  • Very nice of you to say. I was accepted at first, but the sheer poetry of the other answer swayed the result. – duffymo Jul 27 '09 at 09:42
0

Abstract classes can't be instantiated because they lack method definitions.

You could define a junk -- do nothing -- method function in the superclass. But that's silly. Rather than define a "do nothing", don't define anything.

With a body missing, the method is abstract. That makes the class as a whole abstract.


Why?

Because you don't want to specify a "do nothing" method body. You want to omit the method body entirely, leaving a place-holder to be filled by a subclass.

You create superclasses to contain common behavior among subclasses. In some cases, the superclass cannot be a complete class because each subclass must provide an override or extension.

We provide a abstract superclass because we need subclasses to provide the missing features.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
0

Im not sure I understand your question but, an abstract class cannot be instantiated so it only serves this purpose.

theringostarrs
  • 11,940
  • 14
  • 50
  • 63
0

I'll give a real world example. I have an abstract class call cSourceControl. Then I have a class for Visual Source Safe call cVSS and one for Subversion called cSVN, both inherited from cSourceControl.

Now the abstract class has a bunch properties and methods (yes, with code) that the inheriting classes can just use. The abstract class also defines a bunch of abstract methods that an inherited class must implement:

public abstract DataTable getFiles(string path, string filter, DateTime since, bool filterAuthorByLastCheckin, bool expandAll, bool onlySinceBranched);
public abstract DataTable getFiles(string path, string filter, DateTime since, string lastUser, bool filterAuthorByLastCheckin, bool expandAll, bool onlySinceBranched);
public abstract long getFile(string sFileName, string sVersion, string sLocalFileName);
public abstract DataTable getFileVersions(string sFileName);
public abstract DataTable getDirectories(string path, bool expandAll);
public abstract DataTable getChangedFiles(string path);
public abstract DataTable GetFileLogRevision(string path, string revision);
public abstract DateTime getBranchStartDateTime(string sBranch);

From this you can tell that a SourceControl class will need each of these, but the way it looks in cVSS is very different than how it looks in cSVN. Another payoff is that at runtime I can select VSS or SVN. A simplified code snip might be:

cSourceControl sc;

if(usingVSS)
   sc = new cVSS();
else
   sc = new cSVN();

DataTable dtFiles = sc.getChangedFiles("myproject/branches/5.1/");
etc.

Now when my customers start asking for Git or SourceGear, I just have to create those classes and change very little else.

JBrooks
  • 9,901
  • 2
  • 28
  • 32
0

Abstract classes are for outside stakeholders. One procedure is set by managers as abstract is easily visible to stakeholders is company will open on 9am. But implement one extra rule for software developers that they may come 10am. So rules set by managers and below are implemented by abstract and easily visible. 6pm is exit from the company. You can see abstract methods and can know the culture of company.

Rashid
  • 11
  • 4
  • please, if you are trying to explain then... separate actual concept from your example. It looks like spamming in here – Vikrant Jan 22 '19 at 12:17