0% found this document useful (0 votes)
20 views

Abstract V Interface

An interface defines common behaviors for unrelated classes but provides no implementation, while an abstract class can define both common behaviors and provide partial or complete implementations. Abstract classes are better than interfaces when classes need to share common behaviors and status, as abstract classes allow defining access modifiers, fields, and default implementations to promote code reuse. Interfaces are better than abstract classes when classes only need to share signatures as interfaces impose less restrictions and allow for easier addition of new methods without breaking existing implementations.

Uploaded by

chetanraj07
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Abstract V Interface

An interface defines common behaviors for unrelated classes but provides no implementation, while an abstract class can define both common behaviors and provide partial or complete implementations. Abstract classes are better than interfaces when classes need to share common behaviors and status, as abstract classes allow defining access modifiers, fields, and default implementations to promote code reuse. Interfaces are better than abstract classes when classes only need to share signatures as interfaces impose less restrictions and allow for easier addition of new methods without breaking existing implementations.

Uploaded by

chetanraj07
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Feature Multiple inheritance Default implementation

Interface A class may inherit several interfaces. An interface cannot provide any code, just the signature.

Abstract class A class may inherit only one abstract class. An abstract class can provide complete, default code and/or just the details that have to be overridden. An abstract class can contain access modifiers for the subs, functions, properties An abstract class defines the core identity of aclass and there it is used for objects of the same type.

Access Modfiers

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovableinterface. If various implementations only share method signatures then it is better to use Interfaces.

Core VS Peripheral

Homogeneity

If various implementations are of the same kind and use common behaviour or status then abstractclass is better to use. Fast

Speed

Requires more time to find the actual method in the corresponding classes. If we add a new method to an Interface then we have to track down all the implementations of theinterface and define implementation for the new method. No fields can be defined ininterfaces

Adding functionality (Versioning)

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. An abstract class can have fields and constrants defined

Fields and Constants

You might also like