当前位置: 动力学知识库 > 问答 > 编程问答 >

oop - Use of abstract and interface

问题描述:

This is very basic but thoughtful question i come across in many interviews Why we really want abstract classes and interface even we can make there implementation by simply inheritance.

(technically saying by making functions and overriding it).what would left without creating abstract classes and interface in OOPS

Thanks

网友答案:

Abstract classes allow you to have a foundation stone for the rest of your code. You can create a partially implemented class and then allow users to create concrete instances of it that are more specific to their requirements. The common functionality stays in the abstract class. A real world analogy is a car hire shop. They hire 'cars'. The notion of a 'car' is an abstract class and can have various methods in their system that allow hire and return, and also properties like insurance group, daily rate etc. However no customer would go up to a hire shop and hire a 'car'. They'd hire a Ford Focus, a Mercedes, a Skoda Fabia or whatever. So these are concrete instances of an abstract class, and might well be represented in an object-oriented system with specific classes derived from 'car'.
Interfaces, on the other hand, allow polymorphism. This is when an object of one class gets to behave like one of another. I'm a man who drives a car. I am not a 'driver' object (as I do lots of other things), but I might well be regarded as having a 'driver' interface which would allow access to my driving license details and the suchlike.

网友答案:

Abstract classes could contains the partially implementation of some functionality with difference with the interfaces which does not contain any implementation but just pure virtual methods declarations. The basic reason to use an interface is if you really don't know anything about the class with except the how to interact with it (this will give more abstraction to your code) - interface it more abstract than abstact class. Abstact class on the other hand is just a partially representation of an physical object. Also in c++ interfaces could be export from dll with the difference with abstract, or simple classes.

网友答案:

This is a very foundational subject in object oriented theory and practice. Why we have abstract class, why we have interface, what is the difference between the two, etc. There are two aspects to it, which I call the "mechanistic" and the "essential" aspects. When explaining these concepts, usually their mechanistic aspect is stressed (like syntactical differences, you can instantiate or not instantiate it, etc). While, these things are important, more important is to understand the difference in their nature.

I have explained my view of this subject in an article available at my website.

http://nazar-merza.com/

Note: the site is new and still under construction, so there are not many postings yet, but soon I will post some articles on essential aspects of object orientation.

分享给朋友:
您可能感兴趣的文章:
随机阅读: