Programming in Any Language

C# OOP - Lessons


Inheritance

Click to enlarge

This is the first video exploring object-oriented programming. There are a number of topics to consider when exploring OOP. The first one we look at is Inheritance.

Inheritance, I believe, is easy to understand. If you build something, that something has certain properties – could be height, width, depth, color and many other things. Now, if you take that thing and build something new with it, that new thing contains all the properties of the first thing plus any new properties you might have added.

Example. You build a dog house. Four walls, a pitched roof and a door in the front wall. You call it “Dog House”. Now, suppose you make an identical dog house but add front porch for the dog to rest on. We’ll call that the “Luxury Dog House”. It has all the properties of the first dog house with the added property of a nice porch. That’s inheritance.

This video show you how we can extend classes using inheritance.

PDFs: Slides, Car Class, Vehicle Class, Form Code


Encapsulation

Click to enlarge

Encapsulation is also known as Data Hiding. We’ve been doing that in the Structures section and the Classes section. However, when you open up a book on programming and jump to the OOP or Object-Oriented Programming section you usually find Encapsulation there, along with Inheritance, Abstraction, and Polymorphism. Call it the big word section. Sometimes I wonder if the powers that be use the big words just to impress the rest of us. Be that as it may, encapsulation, as I said before, is simply data hiding.

Some of what you will see in this video you have seen before in the classes section – especially the intro lesson and the properties lesson. But this video concentrates on containing data within a single unit and shielding it from the outside world. The video is more of a Why than a How.

I look at Object-Oriented programming as more of a necessity with working with teams of other programmers. You create your class and let others use it. Meanwhile you have the piece of mind that as others use the class the data will be protected through the rules you set up when developing your class.

If you are a lone programmer, there is no one making you use OOP. However, it is good practice to follow.

PDFs: Slides, Class Code,  Form Code


Polymorphism

Click to enlarge

Polymorphism refers to an object’s ability to take different forms. Basically, it allows you to have methods with the same name in the base class as well as their derived classes. You also have the ability to override the properties and methods of the base class.

This video explains all this with two different examples. In the first example we create an animal class that has a “make sound” method that only works for some animals. A derived class like dog and cat overrides the “make sound” method with sounds more in line with a dog and a cat.

A second example uses a rectangle for the base class. Its properties are length, width, and area. Works great for rectangles but in order to go three dimensional we add height as a property of the derived “box” class and override the area property.

PDFs: Slides, Animal Class, Cat Class, Dog Class, Animal Form Code, Rectangle Class, Box Class, Area Form Code‍ ‍

Click to enlarge


Abstraction

Click to enlarge

As you’ll soon see, Abstraction is a way of defining almost a checklist of properties and methods that belong to several sets of objects you need to define. Basically, you create an abstract class which is never intended to be instantiated. In fact, it can’t be. But once created you use it as the basis for several derived classes.

In our video example we are going to define an abstract class based on Recreational Vehicles. We won’t instantiate the class. Rather we will derive three classes that share the properties of the base class. We will set up the large class A motorhome as one derived class. We will also set up a class B motorhome which is actually a converted van. The class C motorhome is based on a truck frame. All three have similar characteristics found in the base class but are very different vehicles.

PDFs: Slides, BaseRV Class, ClassA Class, ClassB Class, ClassC Class, Form Code

Click to enlarge