Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Whats the difference between abstract classes and interfaces. And when to use abstract classes and when to use interfaces in architectural design?

Lets learn something about architectural planning and software development methodologies.

user-image
Question ajoutée par Mohammed Rizwan Afgani , Software Engineer, Team Lead , Prosoft e-Solutions (I) Pvt Ltd
Date de publication: 2013/07/02
Farhad Bheekoo
par Farhad Bheekoo , Senior Solution Architect , Woolworths NZ

- An abstract class is a class that cannot be instantiated.
It can be inherited without having to implement any of the abstract methods.
An abstract class just as any other class can only be inherited once per derived type.
An abstract class is generally used as a base class that does not or should not be instantiated.
It allows you to enforce certain base functions, base properties, provides scalability and groups related attributes.
- An interface does not have any concrete implementation and you have to implement each member.
You can inherit multiple interfaces per derived type.
An interface does not allow for any concrete implementations.
I personally use interfaces when I want to enforce certain functions and also to promote loose coupling As a matter of interest - The speed of an abstract class is faster than an interface as it takes more time for the interface to find the methods for the corresponding class.

Utilisateur supprimé
par Utilisateur supprimé

The abstract class can have methods with or without implementation while the interface does not ,the same goes for constructors , also an interface can support multiple inheritance while abstract class cannot.

Raghavendra Prakash
par Raghavendra Prakash , Senior Architect , Mindtree Limited

If you know when to use, differences will be apparent.
When you want your team to work on implementation, but not deviate from base behavior when not implemented, you use Abstract class.
Your instantiated object will still have default behavior.
However, you cannot instantiate from abstract class itself.
When you dont care for base behavior to be available, you provide interfaces.
Interfaces are especially relevant when you have different teams working on the spec.
Interfaces form the service contracts.
Ex: You plan to start BMW car manufacturing unit at your place.
So, you get abstract specifications of BMW.
You dont need to implement all the behavior by yourself into that.
But, when you produce a car with that spec, in your plant, its BMW car.
Say, you want to provide voice commands in Arabic, so you enable voice processing as an interface, so that it can be changed any other local language, as required.
Hope this helped.

Ayaz Anwar
par Ayaz Anwar , Senior Software Engineer , workforce software (www.workforcesoftware.com)

In general interfaces are prefered over abstract classes for a better design.
Abstract class only make sense when you have common code and object state that you want to share across all child classes through inheritance; other than that interfaces are preferred as it enforces a clean contract.

Mohamed Ramadan
par Mohamed Ramadan , Software Developer , Operational Unit for Development Assistance (OUDA)

Simply: In interfaces : you use just the prototypes of your methods that should be implemented in other classes that implement the interface.
In abstract classes : the class is not complete where there is some method or more that should be implemented after extending this class thanks ! :)

An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class.

An interface is an empty shell, just only the signatures of the methods. The methods do not contain anything. The interface can't do anything. It's just a pattern. An Abstract class is a class which will contains both definition and implementation in it.

 

More about....Abstract Class and Interface

 

Jomy

 

Sadaf Ishaq
par Sadaf Ishaq , Receptionist , Hafeez knitwear pvt.ltd

i dont now about this