Interface VS Abstract classes
-
I have an nterview where I was asked What is useful to use interface or abstract classes. When We use interface and when we should use abstract classes? Thanks in advance
seema
And what did you say?
-
I have an nterview where I was asked What is useful to use interface or abstract classes. When We use interface and when we should use abstract classes? Thanks in advance
seema
An interface can be used in multiple, unrelated classes ( think IDisposable ). An abstract base class allows you to store a base type, for example, whenever I write paint tools, I use an abstract base type 'Tool' class, and store an instance of it in the control that uses it, it's always a type of tool, but the base class gives me a way to store any of them. I've actually never had an interview for a company that didn't end up offering me a job. My first ever interview was the only one I failed, and I bought books and learned all I needed to go back a year later, and get a job from them. You need to move past the specific questions they asked and ask yourself what your inability to answer them tells you about broader areas in which you need to improve your knowledge.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
I have an nterview where I was asked What is useful to use interface or abstract classes. When We use interface and when we should use abstract classes? Thanks in advance
seema
The usage of interface is like a contract. That is to use it to control the entry port and the output to fit some rule that you need. And abstract class is like a context. It's containing the common attributes and deflaut behaviors those could be extented in the subclasses. When we use it is meaning how do we use interface and abstract classes. Use interfaces when something will change frequently. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent for it. It let you define some behaviors, they force your subclasses to provide others Jeff
-
I have an nterview where I was asked What is useful to use interface or abstract classes. When We use interface and when we should use abstract classes? Thanks in advance
seema
Hi Seema, Interfaces are essential when programming using Component Orientated Design as opposed to OOD. In general I tend towards using Interface inheritance as I code for components, not objects. Components are more flexible and reusable, in no small part due to the way they interact using Interfaces and thus separate interface from implementation, making reimplementation or reuse easier. Interface based inheritance means simplifies greatly the use of inheritance and polymorphism as abstract classes may implement member variables or provide virtual functions as well as abstract functions which can make the hierarchy more fragile and prone to reuse issues. If you need common functionality across a number of subclasses then an abstract class is useful; it is, however, also useful to make that abstract class implement an interface for its subclasses to use as their contract with their clients as well. A class may only implement one base class, but can implement multiple interfaces. If an interface implements another interfaces then classes that implement that interface will have to implement the methods for both. Conflicts may be resolved via explicit interface inference or by channeling both into one method (or a combination). As with abstract classes, interfaces may be Generic and decorated with attributes. Interfaces are a very useful and powerful construct. I would recommend their use heavily to achieve a component based architecture rather than an object based architecture. Toby Toby Russell