Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Abstract Class implementing an Interface

I've seen the following code layout reading forums and other blog posts and adapted in order to ask a few questions.

public interface IService<T> {     int Add(T entity);     void Update(T entity); }  public abstract class ServiceBase<T> : IService<T> {     public int Add(T entity) { ... }     public void Update(T entity) { ... } }  public interface ICarService : IService<Car> { }  public class SomeBaseClass : ServiceBase<Car>, ICarService {     public int Add(Car entity);     public void Update(Car entity); } 

What I don't understand is the benefit of having the abstract class implmenting the interface. To me it just feels a bit repetitive and I cannot understand the benefit of having an abstract class implementing the interface.

  1. Why doesn't the abstract class ServiceBase<T> just define as is without the need to inherit the IService interface? Is this doubling up the code?
  2. Why must the SomeBaseClass also implment the ICarService? Shouldn't the ServiceBase be sufficient?
like image 717
fes Avatar asked Dec 30 '12 15:12

fes


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

Ad 1: The additional abstract base class allows you to evolve the interface without breaking the implementation. Supposed there was no abstract base class, and you'd extend the interface, let's say by adding a new method. Then your implementation was broken, because your class does not implement the interface any longer.

Using an additional abstract base class you can separate this: If you add a new method to the interface, you can provide a virtual implementation in the base class and all your sub-classes can stay the same, and can be adopted to match the new interface at a later point in time.

Moreover, this combination allows you to define a contract (using the interface) and provide some default mechanisms (using the abstract base class). Anyone who's fine with the defaults can inherit from the abstract base class. Anyone who wants super-duper fine control about any little detail can implement the interface manually.

Ad 2: From a technical point of view there is no NEED to implement the interface in the final class. But this, again, allows you to evolve things separately from each other. A CarService is for sure a Service<Car>, but maybe it's even more. Maybe only a CarService needs some additional stuff that should not go into the common interface nor into the service base class.

I guess that's why ;-)

like image 155
Golo Roden Avatar answered Sep 29 '22 23:09

Golo Roden