Welcome back to inheritance and polymorphism. This is the fourth course in the introduction to computer programming with Visual Basic specialization. In this module, I want to look at inheritance and inheritance is a way to implement that is-a relationships that we modeled in a previous course when we were doing our UML diagrams. Some learning objectives. By the completion of this module, I want you to be able to do several things. One is to describe inheritance in the in.NET Framework classes. You should also be able to develop computer programs that utilize inheritance. I want you to also be able to develop computer programs that throw and catch exceptions, and we handle exceptions here because inheritance's a key feature of the exception handling in.NET. Let's have some fun. In this first lesson, let's think about inheritance in the.NET Framework classes. System.Object. All objects in.NET extend from System.Object. What this means is that all classes you create in Visual Basic are system objects. Every method in system objects is available in every class you create. There are several methods that you get out of the box. If you create a clean class, you'll have these methods, the tostring method. That converts your object to a string. Now the default implementation is not very good. It basically just has a unique identifier. You also have two equals, there's an equals with a single object and an equals with two objects. The single object you're comparing the current instance to another instance of an object, the two objects where you're comparing those two objects, object 1 and object 2. ReferenceEquals is you're comparing essentially the memory address of the two objects. You also have other methods like GetType, which returns the type of the object, the GetHashCode, which is the unique identifier for the instance of the object, Finalize is what's called when the objects being destroyed and MemberwiseClone allows you essentially to create a clone of an instance of an object's methods. The MyBase keyword. This essentially behaves like an object variable that refers to the base class of all the current instances of a class. MyBase is frequently used to access base class members that are overridden or shadowed in a derived class. You're in an [inaudible] class, you're specializing a class and you want to get access back to the generalizations versions of the class. MyBase.New Is used to explicitly call a base class constructor from a derived class. Again, you're specializing a class and you want to make sure you call the constructor from a generalized version, you can use that MyBase.New constructor. A couple of keywords here, the MyClass and the Me. The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing. The MyClass keyword behaves like an object variable that refers to the current instance of a class as originally implemented. MyClass resembles Me, but every method and property call on MyClass is treated as if the method or property were NotOverridable. Essentially, if someone has created a derived version, you're going to call the original version. The method or property is not affected by overriding in a derived class. First thing we're going to do is distinguish between overriding and overloading. Overriding occurs when two or more methods in one class have the same method name but different parameters. You can have overloading on the same level of generalization if you will. Because the compiler could distinguish which version you're calling based on the parameters. If one method has two parameters, one has three, it can figure it out. If one has an integer and another has a decimal, I can figure it out. Overriding allows a child class to provide a specific implementation of a method that is already provided in the parent class. That happens on different levels of the inheritance tree and has the same signature. When you override a generalized version of a method, you're going to have the same parameters in the overridden version. Here's an example of overriding and overloading. Here we've got two versions of equals. I've created a class called Cow and I've got line 2, I've got the number of spots that a Cow has. I'm going to make that an integer, that's a public field. On line 3, I create an overridden version of the equals method. Because out of the box, an object has an equals method comparing to an object. We don't know what that object is necessarily, we have to test it with a type. You'll see on line 3, sorry, line 4, I say, if the type of the object passed in as a Cow, then I'm going to convert it to a Cow in line 5 so that C type converts the object passed into a Cow and then I can then reference the attribute on the Cow which is spots and I'm comparing to see if it's equal to the number of spots. The current instance has. Otherwise, on line 7, I'm comparing the HashCode. I'm just comparing the two hash codes from the two different instances of the object to see if they're pointing at the same memory location. My specialized version of the equals method, what I'm comparing to another object says, if they're both Cows, I'm going to decide if they're equal, if they have the same number of spots. Otherwise, I'm going to see if they're in the same memory location. On line 10, I overload and I'm overloading because I have a different signature, in this case, I'm comparing a Cow to a Cow, not to an object. You'll see the inCow is a parameter of type Cow, in this case, I can just compare the number of spots. That's it for lesson 1. A little review here. Overloading methods is when two methods have different signatures. Overriding methods is when two methods have the same signatures on different levels of the hierarchy and all objects and VB are specializations of the System.Object, meaning they get all of the functionality from system object. All right, I'll see you in the next lesson.