Importing Inherited Implements

What’s the difference between Imports, Inherits and Implements?

Imports
…lets the developer USE a Namespace, using a referenced library; eg. the Math Object is under the System Namespace, so in order to use the functions that it shares, you’ll declare “Imports System.Math” somewhere in your code.You can use any library that you have referenced in your project WITHOUT using Imports keyword by typing the full class path, eg. “a = System.Math.Abs(10.2);”. By using the Imports keyword, however, you can shorten the line of code to just “a = Abs(10.2);”.

Implements
…lets the developer EXTEND a Class, using an Interface. These interfaces usually start with the capital I in front of them, and implementing one of these would force a class to use certain names and types of certain methods and properties. You have to explicitly declare these constraints, or the class will not compile. However: the Interface itself cannot contain any code, it just enforces a pattern so that other objects can call a generic interface to address multiple implementations.

Inherits
…also lets the developer EXTEND a Class, but this time using a base class. The base class are complete classes with working methods and properties. It does not enforce any constraints to the class that’s inheriting it (unlike Interfaces) so you can overload (or choose to ignore) any method you like.

~~~

An Analogy: Everyone can become Wet. Dogs are Animals. Cats are Animals. All Carnivores can eat Meat.

Now, since everyone can access the method MakeWet, then an “Imports System.Water” might have been declared, so that everyone can just call the Method “MakeWet” instead of repeatedly writing “System.Water.MakeWet”.

We can say that both Dogs and Cats implement the interface IAnimal. This way, we can give both of them the method IAnimal.Talk() and force the coder to write code inside the function. Afterwards, since all animals have the same method name ‘Talk()’, we can use them like so:
Zoo.Add(New Dog)
Zoo.Add(New Cat)
For Each Animal as IAnimal in Zoo
Animal.Talk()
End For
// Output:
// ”Woof Woof”
// ”Meow Meow”
However, all animals eat alike. Instead of copy-pasting the eat method on each animal, lets just have them inherit the Base Class clsCarnivore, which automatically gives them the method ‘EatMeat’. Of course, if an animal only eats certain kinds of meat, we can overload the method, but if not, no extra code is needed.

There, and done~! Don’t get confused.. Until next post, Sayonara~

0 comments:

Post a Comment