Class Creation (SDLC) Visual C# Approach regarding Inheritance

First of all it is important to understand that when it boils down to object oriented design you will need to have an understanding of what a class is. Since I am focusing on teaching a visual approach I will provide an example of how this can be imagined in the real world.

First if you haven't done it already I highly recommend to start reading the What you should know section first. Even though that part uses Java and I am focusing on using C# in this example it will still apply to the same concepts since object oriented design carries over from one software language to another.

Okay hopefully you have the basics down now let's move onto the class example.

Think of a class as a place where all other members originate from. So if you think about your parents they would be considered something known as the base class. All of the siblings are connected to that parent class.

Methods, Public, Private & Protected

The members of the class would obviously be the children. Each child is related to the parent class. Let's say though that there is an adopted child. Although this adopted child is raised by the parent(s) biologically it is not truely related to the parents. The adopted child that would not be related to the parent class is known as a private member while the children as considered public since they are related.

These variations between the true children and the adopted children use something called a method. Now also a method can be called from any other place in the program. So imagine if a child is in another room. A parent call call out to them and they will be able to respond to that parent. Now it's time to see this in code. Is your head spinning yet?

learn c# free

So hopefully by now you are learning something about classes and methods. To reiterate all of the sections you see below for bio_son(), bio_daughter(), adopted_son(), adopted_daughter() are the methods. Remember though that the classes that show public are the true siblings, but the members that show private are not. This way you will learn that the biological children can be be called outside of the Parent class by other classes. Supposedly we could say that the children have Aunts/Uncles that are related to them. However the adopted children cannot be called outside of the Parents class (private) since they are not related to the Aunts/Uncles either. There is another method here called bio_grandson. The bio_grandson method is protected so that means it cannot be used or seen by any of the other classes. This is necessary to keep a method so that it belongs to that class only. After all if you had a Bird class would you mix it with a Dog class?

It is time to expand this class to add the relatives just mentioned. Here is another visual example.

c# online training

So now a new class has been created called Relatives. The Parents class can be inherited by the Relatives class. Please note that the inheritance is recognized with the colon (:) next to the new class.

c#

Virtual and Override

I will start with a new class this time before moving on to the next lesson. Methods are meant to perform a task. Think of a task as a verb, which indicates action going on. I started with the original approach because I am hoping that by comparison with multiple examples that this inheritance stuff will start to stick and click in your brain.

learn c sharp programming

The next lesson will show you the importance of using commands called virtual and override. First lets have a look at the modified example.

learn visual c#

The virtual method can be used to change the behavior of the original method called drives_fast() from the base class Sportscar. Then in the Economy class the override method is placed behind the drives_fast() method to change its state. As you can see in the above example, this will allow it to implement the properties from the base class to be used in our new class here. Now you can safely build upon this new method. As an example maybe you could create a counter showing how fast the sports car can drives compared to the economy vehicle.

Polymorphism

Now we are moving onto a process known as polymorphism. This is necessary to allow base class variables to have the ability to reference a derived class. It will also remember which object belongs to that class and can use methods that are overrided of the derived class even if the class is unknown. In English that means having the ability to create a variable, function or object with different forms. As usual though an example is due. Think of this as someone who has built a rocket that can travel to the moon. Okay so its not 1969 yet. Anyway polymorphism can be restructured to allow that rocket to travel beyond the moon or even to the other side of the galaxy. We have that technology today since there is a rocket by Nasa called the Nasa New Horizons Spacecraft that is on its way to Pluto. With polymorphism we can incorporate intelligence into our classes. Excuse the Science lesson for a moment. Here is an example of that.

learn c# fast

A couple new things I should mention here. Since the distance from the Earth to Pluto is a very large number, which is a staggering 4,284,726,912 (over 4 billion miles!) then it is necessary to use the keyword long instead of int (integer). This is because the long can hold 64 bit numbers. When you get into calculation of a billion you will find it handy. Throw some cash my way when you do. Just kidding. Also it is necessary to use the keyword new in front of the second modified method or you will get the error message "Use the new keyword if hiding was intended".

Finally the return keyword is necessary since these methods contain new variables (int and long). The easiest way to identify when a return is required is to check if a variable proceeds it first. In the example above the statement new public long Traveling(). Since this public has been initialized with a variable (long) then it must have a return value.

However below is an example shown earlier of a method that requires no return value. Notice that is because there is no variable in this method.

c# tutor

Calling an overloaded Base class constructor

Now you will learn how to call an overloaded method from the base class. First understand that an overloaded method is a method with a set of parameters. If you examine the picture below notice the method called public Generic_Rocket(double speed). The parameters are the variables contained within the parenthesis () which in this case is the variable called speed. Here are the modified classes.

c# training course

Looking at the class called NewHorizons_Rocket: Generic_Rocket you will see the new method added called public NewHorizons_Rocket(): base (52000). This is allowing the base class of Generic_Rocket to use the variable called speed in the place that shows base(52000) to indicate how fast the Nasa rocket is predicted to be traveling in orbit. The this is necessary in the Generic_Rocket class to refer to the current instance of that class (otherwise known as the current object instance).

Subscribe to the Programmermind newsletter
Privacy Notice: Programmer Mind will not compromise your privacy