Technical reason for using abstract classes in C # / Java

According to OOP, abstract classes are needed to model those objects that do not exist in the real world, but serve as the base class for several real-world objects.

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 reason for using OOP to use interfaces is to model behavioral inheritance (Inheritance without a real hierarchical relationship).

The technical reason for using interfaces in C # / Java is to solve the problem of multiple inheritance (if I'm not mistaken!).

0
abstract-class
source share
5 answers

Abstract classes may have a default behavior, if reasonably possible; interfaces cannot.

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

Abstract classes can have a state shared by all subclasses; interfaces do not indicate status.

Thus, your abstract BankAccount may have a balance attribute that can be granted access to Savings and Checks.

+8
source share

Abstract classes cannot be created because they do not know the definition of methods.

In the superclass, you can define the function of the unwanted work - do nothing - method. But this is stupid. Instead of defining "do nothing", do not define anything.

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


Why?

Because you do not want to indicate the body of the "do nothing" method. You want to completely lower the body of the method, leaving the place holder with a fillable subclass.

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

We provide an abstract superclass because we need subclasses to provide the missing functions.

0
source share

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

0
source share

I will give an example of the real world. I have an abstract call to the cSourceControl class. Then I have a class for Visual Source Safe call cVSS and one for Subversion cSVN, both inherited from cSourceControl.

Now an abstract class has properties and methods of binding (yes, with code) that only inheriting classes can use. An 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 say that each of them will be needed by the SourceControl class, but the way it looks in cVSS is very different from how it looks in cSVN. Another benefit is that at runtime I can choose VSS or SVN. A simplified snapshot of the code could be:

 cSourceControl sc; if(usingVSS) sc = new cVSS(); else sc = new cSVN(); DataTable dtFiles = sc.getChangedFiles("myproject/branches/5.1/"); etc. 

Now that my clients start asking for Git or SourceGear, I just need to create these classes and modify them a bit.

0
source share

Abstract classes are for external interested parties. Managers establish one procedure, because the resume is easily visible to interested parties if the company opens at 9:00. But implement one additional rule for software developers that they can arrive at 10 in the morning. So the rules set by managers and below are implemented abstractly and are easily visible. 6 pm exit from the company. You can see abstract methods and learn the culture of the company.

0
source share

All Articles