DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report

Understanding abstract classes

Denzel D. user avatar by
Denzel D.
·
May. 07, 10 · Interview
Like (0)
Save
Tweet
Share
6.76K Views

Join the DZone community and get the full member experience.

Join For Free

A misconception I sometimes encounter among beginner developers is the fact that some people do think that abstract classes and interfaces are the same thing. However, this is a wrong understanding. Although abstract classes and interfaces have similarities, their purpose as well as structure is different.

Abstract classes can have method/function implementations

The interface is a structure, that allows the developer to outline a set of methods, properties or and/or indexers that should be implemented in a class that implements the interface. Therefore, if an interface defines the Add and Remove methods, the inheriting class must have those methods implemented. One important aspect of interfaces is that no implementation is provided for the declared methods and functions – it all has to be done in the class that inherits the interface.

Abstract classes, on the other side, can (but not necessarily) have method/function implementations, providing default functionality for them. Therefore, once a class inherits an abstract class (in that case referenced as the base class), the developer can either use the default functionality or override the existing methods/functions.

A class can inherit multiple interfaces, but only one abstract class

C# is a language that does not support multiple inheritance. And although a single class can inherit multiple interfaces, it cannot inherit multiple abstract classes. It can be considered a limitation, but in fact it isn’t. An interface gives the freedom to develop anything that is built around a well-defined structure, while an abstract class sets a hierarchy that should be followed.

Let’s take a look at an example. Here I have an abstract class SystemDetails:

public abstract class SystemDetails
{
public string GetDetails()
{
string version = Environment.OSVersion.VersionString;
string machineName = Environment.MachineName;

return string.Format("{0} is using {1}", version, machineName);
}

public void CheckTime()
{
Debug.Print(DateTime.Now.ToString());
}

public abstract void GetUserInfo();
}


This class explicitly defines the default functionality for GetDetails and CheckTime. At the same time, it defines an abstract method – GetUserInfo, that doesn’t carry any implementation (just like in an interface).

A class that will inherit SystemDetails can use the default functionality of GetDetails and CheckTime, but must also provide an implementation for GetUserInfo. Here is an example:
public class MySystem : SystemDetails
{
public override void GetUserInfo()
{
Debug.Print(Environment.UserName);
}
}
As you see, I am not providing an implementation for GetDetails and CheckTime inside the MySystem class. However, when I will instantiate the class, I can call both GetDetails and CheckTime, since those are already implemented in the base class.

As GetUserInfo is an abstract method, I had to provide an implementation. The difference (compared to interfaces) is that I have to override the existing method declaration, even if no implementation is provided in the base class.

Using the same override keyword, I can override the existing methods, properties and functions, providing new functionality.

Access modifiers

Compared to an interface, abstract classes can have members that have different access modifiers. In an interface, all elements are set as public and that cannot be changed. In an abstract class, the developer can use different access modifiers (like private, protected or internalprotected). This would mean that a lot of class members can be used internally, without those being exposed.

The question – when to use interfaces and when – abstract classes?


This is one of the questions that comes up in many interviews, but also when a developer tries to decide what’s better for a specific case. When you want to set a base structure that will apply to classes of the same type, then use abstract classes. An interface is applied to classes with diverse types and no well-known structure, that can have different functionality (for example, the same method declared in an interface can be implemented differently in two classes that implement the same interface). And although in an abstract class the methods can be overridden, if you intend on providing default functionality for a multitude of classes, then abstract classes is the way to go.


Interface (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Real-Time Analytics for IoT
  • Getting a Private SSL Certificate Free of Cost
  • Introduction To OpenSSH
  • Rust vs Go: Which Is Better?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: