简体   繁体   中英

Why we use abstract class and interface ? Difference between Class, Abstarct Class and Interface

I know this is very repetitive question i also read most of the post but not found the satisfactory answers. I know the bookish difference which i read from most of the posts.

Can any one please tell me

why we use abstract class and interface?

when we use this one?

i know that when we have some repeated task but implementing in a different way each time then we use abstract class.

i think we can implement this by a normal class and subclass using overriding then why we use abstract? also their is a difference of access specifier between abstract class and interface as well as the compulsion of implementation in interface .

Interface is quite acceptable that when we want to force to implement all method in that situation we use interface but why abstract class? Is compulsion of implementation is just a difference?

thanks in advance.

Beautifully explained here - https://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface

In a nutshell,

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Also take a look at https://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx

Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.

  1. If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

  2. If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

  3. If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

  4. If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

Consider this.

An interface has no implementation. It forces prototypes onto a class but does not help implementing them.

An interface is not tied to a class hierarchy. You can apply the same interface to multiple, entirely different class trees. This is important and one of the things that an interface offers over an abstract class. An interface is implementation independent, you can "slap it onto" any class. This is the most powerful aspect of an interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM