An object-oriented program is a collection of interacting objects which are capable of receiving messages, processing data, and sending messages to other objects. Each object can be viewed as an independent "machine" with a distinct responsibility. The actions on these objects are closely associated with the object. For example, OOP data structures tend to carry their own methods around with them.
VB.NET / C# supports the four major defining concepts required for a language to be fully object-oriented:
- Abstraction - Class keyword
- Encapsulation - Interface
- Polymorphism - Method Signature
- Inheritance - A new class can be based on an existing class
C# Absract class
public abstract class Car { public abstract void drive(); public virtual void fillgas() { Console.WriteLine("Fill Gas"); } }
C# Interface
public interface IVehicle { void Start(); void Drive(); void Park(); void ChangeGear(int gear); void SwitchOff(); }
What's the difference between abstract class and interface?
An abstract class is a class that can not be instantiated but that can contain code.
An interface only contains method definitions but does not contain any code. With an interface, you need to implement all the methods defined in the interface.
Class can only extend one class but implement multiple interfaces.
A good way to to make a desision between abstract class and interface:
Are there many classes that can be grouped together and described by one noun? If so, have an abstract class by the name of this noun, and inherit the classes from it. A key decider is that these classes share functionality, and you would never instantiate just an Animal, you would always instantiate a certain kind of Animal: an implementation of your Animal base class. Example: Cat and Dog can both inherit from abstract class Animal, and this abstract base class will implement a method void Breathe() which all animals will thus do in exactly the same fashion. (I might make this method virtual so that I can override it for certain animals, like Fish, which does not breath the same as most animals).
What kinds of verbs can be applied to my class, that might in general also be applied to others? Create an interface for each of these verbs. Example: All animals can be fed, so I will create an interface called IFeedable and have Animal implement that. Only Dog and Horse are nice enough though to implement ILikeable. I will not implement this on the base class, since this does not apply to Cat.
The main difference is where you want your implementation. By creating an interface, you can move your implementation to any class that implements your interface. By creating an abstract class, you can share implementation for all derived classes in one central place, and avoid lots of bad things like code duplication.
When you can fully describe the concept in terms of "what it does" without needing to specify any of "how it does it", then you should use an interface. If you need to include some implementation details, then you will need to represent your concept in an abstract class.