Cloud Hosting

The Internet of Things: A Cloud Frontier That’s Being Settled Today (Part 2 of 2)

Sam Guiliano July 19, 2016 by under Cloud Hosting 0 Comments

Go to Part 1

  • Let’s Make Some Magic with the IoT (continued)
  • Internet of Things Too Powerful for Cloud?
  • The Ability to Process Data in All its Diversity
  • Different Types of Standards
  • Security in the IoT
  • The Automation of Things
  • Cloud Hosting to Fuel Your IoT Transition

Let’s Make Some Magic with the IoT (continued)

Cloud technology was designed for an Internet of Things world, argues Jamie Carter of TechRadar. The structural approach to computing makes it much easier to achieve interoperability between many different devices and systems, a feature that becomes increasingly complex but nonetheless fundamental as the IoT expands.

Cloud has been growing incredibly as businesses have shifted from entirely Windows environments to Mac and mobile, according to CSID chief innovation officer Adam Tyler. He adds that the technology will become even more prevalent as the Internet of Things continues to build.

Read More


The Internet of Things: A Cloud Frontier That’s Being Settled Today (Part 1 of 2)

Sam Guiliano June 22, 2016 by under Cloud Hosting 0 Comments

The cloud and Internet of Things are often referenced in conjunction. The IoT is considered an application of cloud computing by many. While the Internet of Things does require the cloud to operate, its scope and power will have a major impact on how cloud computing develops.

  • Deep Connection between Cloud and IoT
  • Big Data of Things will Empower the Cloud
  • Let’s Make Some Magic with the IoT
  • Harnessing the Internet of Things with Cloud Servers

Deep Connection between Cloud and IoT

Every January, one of the biggest tech events in the country is held in Las Vegas: the Consumer Electronics Show. As expected for the 2016 event, there was an increasing amount of cloud discussion among companies and attendees. The majority of new electronic products, ranging from vehicles to kitchen appliances, are integrated with the cloud. Gradually the technology is becoming more ingrained throughout industry and, in turn, throughout our lives.

Read More


Like Marty McFly, You Can Future-Proof Your Business Using Cloud and Other Tools (Part 2 of 2)

Adnan Raja June 10, 2016 by under Cloud Hosting 0 Comments

<<< Go to Part 1

  • Improve Your Mobile Stance
  • Bring Your IT Abreast of Time
  • Cloud: It’s Not as Ambiguous a Choice as it May Seem
  • Human Future-Proofing
  • Ready for the Future?

Improve Your Mobile Stance

You need your business to be empowered, or you won’t get very far – and in some ways, you won’t even be functional. Just look at Marty McFly. Realizing he didn’t have any plutonium in 1955, he and Doc Brown had to connect a lightning rod to the flux capacitor in order to get back to 1985. For all Marty’s future-proofing, getting his father to win the affection of his mother, the progress he needed was impossible without a new solution for an old time-travel problem.

Just like Marty needed lightning (and hurry to deliver it, Doc Brown!), you need mobile. Yes, of course mobile is hardware rather than a basic resource, but this part of your business is so increasingly important that focusing on it really will fuel your business’s success.

Read More


Like Marty McFly, You Can Future-Proof Your Business Using Cloud and Other Tools (Part 1 of 2)

Adnan Raja June 9, 2016 by under Cloud Hosting 0 Comments

Marty McFly showed us how to future-proof our own lives when we use time machines. Now it’s up to us to future-proof our businesses through cloud and other new technologies.

  • What is “Future-Proofing” a Business?
  • Beyond Tech: Future-Proofing as a Business Ultimatum
  • Looking at the Future More Broadly
  • Updating Your IT
  • Tip #1: Simplify
  • Tip #2: Purchase Assuming that You Will Grow
  • Adopting a Cloud Server to Embrace the Future

What is “Future-Proofing” a Business?

Suffice it to say that Marty McFly ran into some problems in 1955. The character, played by Michael J. Fox, accidentally drew the attention of his mother and distracted her from his father. Before he went Back to the Future, McFly was in danger of overwriting his own existence! The only answer for him was to rapidly future-proof the world of 1955 so that when he returned to 1985, he would still be alive and well.

Read More


How Elon Musk Stole My Car

Marty January 14, 2016 by under Cloud Hosting 0 Comments

This was my personal experience with Tesla Motors. I’m a fan of what Tesla Motors is trying to accomplish and hope they get their issues worked out.

 

With a new baby on the way, I was in the market for a new vehicle.  I scheduled a test-drive with Tesla Motors (Tesla) in mid-November and was on the fence about purchasing a Tesla. I had some questions which I emailed my test-drive consultant, but didn’t receive any response and I wasn’t particularly in love with the car, so I let it go.

Read More


Why Deis and what is it?

Adnan Raja January 12, 2016 by under Cloud Hosting 0 Comments

Do you want to join the era of open platform-as-a-service? Adopted by Mozilla among others, Deis is one option that combines the strengths of Docker and Chef.

  • Groundwork of Deis
  • DevOps & NoOps
  • Born from Docker
  • Decision to Transition to Chef
  • Hats Off to Heroku
  • Deis on the Rise
  • Commercial Support Now Available
  • Deis Hosting from Industry-Leading Cloud

Groundwork of Deis

If you are a developer, it’s nice to see all the different types of environments and tools that are becoming available to automate the implementation of apps and to manage infrastructural components. With the nuts and bolts handled, it’s becoming easier to really focus squarely on innovation.

One way to leverage new technologies to improve the lives of developers is via a platform as a service (PaaS). This form of cloud computing give them an ecosystem that creates a distinction between the program and the OS.

Read More


A Two-Year Ansible Case Study

Sam Guiliano January 8, 2016 by under Cloud Hosting 0 Comments

Are you interested in finding a new configuration management system? Here are some thoughts from one system administrator’s two years of experience with Ansible.

  • The Search for Easy Configuration
  • Why Ansible over Chef, Puppet & SaltStack?
  • Uses
  • A Few Tips from Experience
  • Cloud from Infrastructure Experts

The Search for Easy Configuration

If you are trying to figure out which configuration management system would work best for you, you may have SaltStack, Puppet, or Chef on your list. We all develop our comfort zones with certain tools, and it can sometimes be a little confusing to figure out which of a variety of environments is best.

Read More


How to: Object-Oriented Programming – More with Classes and Objects

Bjørn Furuknap December 7, 2015 by under Cloud Hosting 0 Comments

Bonus Round

Welcome back to our Introduction to Object-Oriented Programming! Perhaps it would be more appropriate to say, “Welcome to the Bonus Round of our Introduction to Object-Oriented Programming!”. This article and the one that follows it are for those of you who want to learn a little more about some of the foundations of Object Orientation and who enjoy some bonus content!

To review, if you’ve followed along with some of the first five articles in this series, then you’ve learned quite a few things:

This article and the next will take what you’ve learned from that introduction and show you more about how you can use some of the principles of object orientation (often abbreviated OO) to make efficient and well-organized code. We will start with a new program that will allow us to review some basic concepts and lead into some new concepts, such as constructors and inheritance.

Again, we’ll be programming in C# using Microsoft’s Visual Studio. If you’d like to follow along (and haven’t done so already) you can find the setup right here.

If you follow the setup in that article up until the “Under the Hood” section, you’ll be right where we’ll be starting. The only thing I will do differently from that article is that I will not be using the default project name “ConsoleApplication1”. Instead, I will call my project “PettingZoo” (in one word) because we will build a petting zoo as our example program.

Note: Object orientation is available in many languages and platforms, and the concepts are very similar. I have chosen C# because it is easy to get started and all the resources you need are free of charge. You may have other languages and platforms you want to use, and that’s fine, too. We’re here to learn object orientation, not any language or implementation in particular.

.

Initial Setup

Before we begin, verify that you have a console application created in Visual Studio. It should look something like Figure 1.

Figure 1: Initial PettingZoo Program Setup

Figure 1: Initial PettingZoo Program Setup

With this console application available in Visual Studio, we can start creating our first class. In C#, there are multiple ways to create a class. In some languages, such as Java, you need to create classes in specific ways using specific file name and folder patterns, but C# just cares that we say that something is a class.

Note that even in the default console application, you already have one class called Program. Using the keyword class followed by the name of the class is how you define a class in C#.

For our approach, we’ll create a new code file to hold our classes. Right-click on the PettingZoo node in the Solution Explorer pane, and then click Add->Class, as shown in Figure 2.

Figure 2: Adding a Class with Solution Explorer

Figure 2: Adding a Class with Solution Explorer

In the dialog box that opens, find the Code element either by searching or navigating, and call your class file something like Animals.cs, as shown in Figure 3.

Figure 3: Adding Class File Dialog Box

Figure 3: Adding Class File Dialog Box

You should now get a new class file created by Visual Studio that contains something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PettingZoo
{
    class Animals
    {
    }
}

Note that by default Visual Studio creates both a class file and a class definition within based on the file name you chose. In some languages–like Java–this structure is a requirement, but in C#, it’s not so strict. We will change the class name in a moment, but leave it for now.

In the file Visual Studio just created, you will notice a number of using statements as well as a namespace code block. These lines aren’t important to our class, but–in case you’re curious–a namespace in C# is a way to organize names in your project. You can have multiple classes with the same name, for example, organized under different namespaces.

As long as you work inside a single namespace, addressing classes is fairly straightforward. But if you need to organize your project into more complex divisions, you may need to utilize different namespaces, in which case the using statements allow you to create shortcuts to those namespaces.

.

Classes and Objects

The file you now have contains a premade class with a name based on the filename you chose. We’re not going to have an Animals class, though, so let’s change that to something else. In a petting zoo, we need dogs, of course, so change the name of the class from Animals to Dog:

class Dog
{
}

Note the change from Animals (plural) to Dog (singular). We will create another class later called Animal, so be careful when you read the code that you pick up on nuances like that.

Before we start adding any content to the class, let’s examine the difference between classes and objects. Remember that a class is the template for how objects should be. A simplistic and not terribly accurate analogy is that of a recipe and a cake.

(This section is a review of classes and objects adapted from the explanation presented in the linked article above. If you want to skip this refresher and get straight to the programming, jump right to the next section! Otherwise, enjoy some cake!)

The recipe tells you how to make a cake and is thus–in OO terms–a class. A cake is the result of the recipe execution and–again, in OO terms–is an object. Another term used frequently is instance, to indicate that the cake is an instance of the execution of a recipe.

As with cakes and recipes, each cake is a unique cake. What you do to one cake does not affect any other cakes. Putting frosting on one cupcake doesn’t mean all cupcakes now have frosting. Similarly, if you eat one cupcake, you still have all the others left.

However, having a recipe and knowing it produces a cake isn’t enough. You also need a kitchen in which to bake the cake. In OO terms, this is the program where you create the object, and in our case, that would be the main Program.cs file.

.

Back to the PettingZoo

Let’s use our new class to create a Dog object, so open the Program.cs file and add the following line inside the static void main code block:

Dog myDog = new Dog();

This line will create a new Dog object from the Dog class and store it in the myDog variable. Let me break this line down into three parts so I can explain what goes on.

[1]Dog [2]myDog = [3]new Dog();

The first element [1], Dog, is the type of variable we want to create. In statically typed languages like C# and Java, we need to declare what type a variable is before we can assign values to it. Languages that are dynamically typed, like JavaScript or Perl, does not require this and puts the responsibility of tracking what type of data a variable contains on the developer.

The second element [2], myDog, is the variable name we use to access the object. This is not necessarily the name of the dog we create but rather what we use to reference this particular instance of a Dog class.

Note: For more on variables and data types like string, check out our introduction to programming article on variables and types.

.
Finally, the third element [3], new Dog(), tells us that we want to create–or instantiate–a new object based on the Dog class. Notice that there are parentheses after the class name, which means this is actually a method. We’ll get back to this method, which is a special method called a constructor (we’ll go deeper into constructors in the next article linked below).

With our program now creating a new object from the Dog class that it stores in the myDog variable, we can start accessing the properties and method of our object. Except, of course, that our class is completely empty and devoid of content! Let’s fix that.
.

Properties and Methods

Let’s go back to our Animals.cs file, where our Dog class has a name but nothing else. However, everything we put inside this code block will become part of the class definition. Remember, classes have properties and methods that define, respectively, what an object is and what it can do.

Let’s create both a property and a method, and then we’ll discuss what each does. Inside your code block’s curly brackets, {}, add the following two lines:

string Name;
void Speak() { Console.WriteLine("Woof"); }

The first line (string Name;) is a property or field. In object orientation, properties define what something is. In our case, we want our dog to have a name, so we have created a string property to hold the name of the dog.

Remember that object orientation seeks to create programmatic representations of real objects. Of course, a name alone does not make a dog, but OO does not seek to duplicate all properties of every object, just those we need. We will add more properties later.

The second line is a method definition (or method signature) and is a bit more complex, so, just like before, let me break it up a bit and show you what each element does.

[1]void [2]Speak() [3]{ Console.WriteLine("Woof"); }

Element [1], void, is the return type of the method, or the type of output that the method will yield when it is executed. In statically typed languages like C# and Java, in addition to declaring variable types, we must always declare what type of data we return from methods. Dynamically typed languages leave the responsibility of determining what comes out of a method to the developer.

In our case, at least initially, we just want the dog to say “Woof” when we ask it to speak, so we don’t need to return anything. This will change later, when we cover inheritance in the next article.

However, even though we don’t want to return any specific value, we still need to be explicit about it in the program. To do so, we can declare that the method returns a void value, which in C# and similar languages means that we’re not returning anything.

Element [2], Speak(), is the method name, and it is what we use to call or execute the method. Note that methods, like properties, belong to the object.

Element [3], the code block, is the code that the program will execute when we call the method. You may also see this sort of code block also called the method body. Right now, within this code block, we’ll just tell the console to write a line containing “Woof”.
.

Making Your Method Public

Now that we have a method, we can call or execute it from our program and have the dog bark. There’s a problem, however, which gives me a great opportunity to review another of the key concepts of object orientation.

Normally, to access properties and methods of an object, you would first write the name of the variable holding your object and then the name of your property or method. For example, to get the value of the Name string property, you could write:

string dogName = myDog.Name;

To execute the Speak() method, you could then write:

myDog.Speak();

However, if you do this in Program.cs, you will get an error message like shown in Figure 4.

Figure 4: Protection Level Error

Figure 4: Protection Level Error

The error, stating “’PettingZoo.Dog.Speak()’ is inaccessible due to its protection level” happens because of a key concept in OO called abstraction.

In short, abstraction takes away the details of how something works when we don’t need to know those details. In our program, we only need to know that the dog can speak, but do not really care about how it does it.

Abstraction instead allows the class designer to come up with access points into the class through which they can work with the objects. Many languages enable this access through access modifiers.

C#, by default, hides properties and methods from outside of the class. The policy on this sort of separation differs in other languages, though, and some languages like JavaScript do not even offer this level of protection.

However, the principle remains that you make public what you want everyone to use and hide the rest. The making public part, in C# at least, simply means adding the keyword public in front of the properties and methods you want someone to access. So, if you update the Dog class to add public in front of the void Speak() line in Animals.cs, the red squigglies go away in Program.cs, as shown in Figure 5.

Figure 5: Public Method Declaration

Figure 5: Public Method Declaration

As an added bonus, when you make your method public, Visual Studio will add the method to the available options through the IntelliSense autocomplete system. When you type myDog and hit the period, you get a list of all public properties and methods of that object, as shown in Figure 6.

Figure 6: Visual Studio Intellisense Auto-Complete with Our New Public Method

Figure 6: Visual Studio Intellisense Auto-Complete with Our New Public Method

Other code editors have similar auto-complete systems. These auto-completes are one of the main benefits of these editors for programmers: you’ll learn to love these features as you evolve as a programmer!

.
Now that we have fixed all the errors, we should be able to run our program and see that the dog barks. You can place a breakpoint in your Program.cs by clicking in the left margin or placing your cursor on the bracket after myDog.Speak() and hitting F9. Then, hit F5 to run the program. You should see the console pop up, and the output from your dog barking should appear, as shown in Figure 7.

Figure 7: Our Dog Speaks!

Figure 7: Our Dog Speaks!

Of course, now we can create a litter of dogs by creating multiple variables and instantiating objects into them. You can try something like Figure 8, for example:

Figure 8: Two Dogs Speaking

Figure 8: Two Dogs Speaking

Got a similar result? Great, pat yourself on the back, but only once, because we still have more work to do. After all, we’ve been promising that object orientation allows us to create unique objects, and all we’ve done is create two identical “dogs”. Next, we’ll show you how to use constructors to create separate and unique objects.
.
.


How to : Object-Oriented Programming – Constructors and Inheritance

Bjørn Furuknap December 4, 2015 by under Cloud Hosting 0 Comments

Previously

We have almost reached the end of our journey through this bonus round of our Introduction to Object-Oriented Programming! In our last article, we started our Petting Zoo example with a review of classes, objects, and methods. If you missed the beginning of this series, you can catch back up with us here. Otherwise, let’s dive back in!
.

Constructors

If you did the exercise with two Dog objects, it was a bit boring, right? After all, we have nothing to separate the dogs from each other and no way of knowing, without looking at the source code, which dog produced which bark.

In the previous article, I mentioned that when you create objects, you call a special method called a constructor. The constructor looks like the class name written as a method. For example, for a Dog class, the constructor would be called Dog().

The special thing about constructors is that they are the path to any new object, so they are a great place to call code that initializes an object with default values. Further, the return value from a constructor method is always an object of the class itself, which is why we can assign the return value of the constructor to a variable of the type of class we create.

However, so far, we have not actually created a constructor at all, so how come we can still call that method?

In many languages, C# included, the language gives you a free and empty constructor without you having to do anything. It is implied that you want a constructor; otherwise there would be no way of using the class for anything, so the languages just assume that you have written one.

This invisible and free constructor is called the default constructor, and, in our example, it will look like this:

public Dog(){ }

Notice that this syntax is very similar to the Speak() method we created earlier, except that we do not explicitly return a value nor do we even declare the return type of the method. As I mentioned earlier, a constructor always returns an instance of the class to which it belongs.

In this case, that is the class Dog, and that is why when we write Dog myDog = new Dog(), we can assign the new object to a variable named myDog which is of type Dog.

So let’s add the default constructor to our Dog class. You can either copy the line above or, in Visual Studio, you can use a shortcut: type ctor and hit Tab twice. It should generate the default constructor for you, as shown in Figure 9:

Figure 9: Adding a Constructor with 'ctor'

Figure 9: Adding a Constructor with ‘ctor’

The default constructor doesn’t actually give us anything new because it is now explicitly doing what was done implicitly before. However, it is a method, so we can now add content inside the brackets that will execute whenever we call this constructor. And because the constructor runs as the very first thing in an object’s construction, it is a perfect place to add initialization code.

For example, we could set the Name property of our objects to something by adding code such as this:

public Dog()
{
    this.Name = "Snoopy";
}

This example will set the Name property of any new objects to “Snoopy”.

Of course, that’s not very useful because not all dogs are called “Snoopy”, so instead, let us change the method signature of the constructor so that it accepts a parameter.

The parentheses of methods aren’t just there to look pretty; they serve to contain parameters that we can use to pass values to a method. This function applies to all methods, not just constructors, but let’s do it for a constructor first.

Change the default constructor signature to this:

public Dog(string dogName)

This addition allows us to send a string parameter into the constructor, and that when we do, we can refer to that parameter by the name dogName.

Then, add the following line to the method block:

this.Name = dogName;

This line sets this object’s property Name to the parameter we sent into the constructor.

Note that when you change the constructor’s signature, you get a case of the red squigglies in your Program.cs file, as shown in Figure 10.

Figure 10: A Case of the Red Squigglies from Our New Constructor

Figure 10: A Case of the Red Squigglies from Our New Constructor

When we add our own explicit constructors, C# and .NET will not implicitly create a default constructor for us. In our Program.cs file, we are still creating the Dog objects using the default parameter-less constructor, which now no longer exists.

To fix this problem, we need to add a parameter to our constructor call in Program.cs. We can, for example, update our object construction line as such:

Dog myDog = new Dog(“Snoopy”);

Doing so will remove the red squigglies and allow you to run the code again. If you leave or set your breakpoint after the last code line, you can look at the Locals panel and verify that your object’s Name property has indeed been set, as shown in Figure 11.

Figure 11: Seeing our Name Property Set Correctly

Figure 11: Seeing our Name Property Set Correctly

Got it? Good! We now have the ability to name our dog, but it isn’t really a useful program if people have to debug it to see what we’re doing. So let’s mix up the code a bit to make sure that we display the name of the dog that is barking.

Update your Dog objects and change the Speak() method as such:

public void Speak() { Console.WriteLine(this.Name + " says: Woof"); }

The change we have made now tells the WriteLine method to concatenate the name of this object with the literal string “ says: Woof” which should give us output that better displays our efforts. Try running the program now, and you should see something resembling Figure 12.

Figure 12: Snoopy Says: Woof

Figure 12: Snoopy Says: Woof

Nice work! You can now create many dogs with different names, and they will all bark at your command.

Of course, a petting zoo with only dogs is somewhat boring. I mean, I love dogs, but maybe we could add some additional animals to spice up the experience a bit?
.

Inheritance

Let’s start out by adding a new Cat class to our Animals.cs file. Add the following code next to the Dog class, but still inside the PettingZoo namespace brackets:

class Cat
{
    public Cat(string catName)
    {
        this.Name = catName;
    }
    string Name;
    public void Speak() { Console.WriteLine(this.Name + " says: Meow!"); }
}

Then, let’s create a Cat object in Program.cs and have it speak:

Cat myCat = new Cat("Garfield");
myCat.Speak();

Running this program now should give you output resembling Figure 13.

Figure 13: Garfield Says: Meow

Figure 13: Garfield Says: Meow

Our program is predictable and works, but did you notice how similar the two classes really are? See how much code we’ve duplicated across the two classes? Wasn’t object orientation supposed to save us from writing code multiple times?

The answer is yes. We can–and should–simplify this code to reduce the amount of duplication. What we are about to go into will demonstrate a feature of OO called inheritance.

Inheritance in object orientation allows you to create a hierarchy of classes and have each class inherit the properties and methods of a parent class. For example, for our petting zoo, we can create a parent Animal class and have Dog and Cat inherit from that class. Then, we can move parts of our code into the Animal class so that both the Dog and Cat classes will inherit that code.

If, however, we move the duplicate parts of our code to this Animal class, then the Dog and Cat classes would each inherit the same properties and methods, making the children clones of the parent classes. This organization isn’t very useful because we want to have distinct types of animals. It would be terribly boring if cats, dogs, and all other animals were the same. In fact, for the purposes of our program, it wouldn’t make sense to call them different things at all.

Now, if inheritance meant that we could only create child classes that were identical to their parent classes, there wouldn’t be much point to going to all of this effort. So while we inherit all parts of a parent class, we can also override certain parts of a parent class in the child classes to make the child classes distinct.

Let’s go through this and see how it works, shall we?
.

Creating Parent and Child Classes

We’re going to take a few steps back and recreate the Dog and Cat classes in a better way. To start off, we need a parent class that will define the shared properties and methods of the child classes.

In your Animals.cs file, remove the Dog and Cat classes and add the following class definition:

class Animal
{
    public string Name;
    public string Sound;
    public void Speak() { Console.WriteLine(this.Name + " says " + this.Sound); }
}

This class looks unsurprisingly like the previous Dog and Cat classes, with the noted exceptions that we have made all properties and methods public and that we have introduced a new property called Sound of type string. Finally, we have updated the Speak method to use the Sound variable.

At this point, your Program.cs will not work and, if you click on that tab, you should see several error messages, which is only natural because we have removed the Cat and Dog classes. Let’s recreate them and do so using inheritance. In your Animals.cs file, right after the Animal class, add the following code:

class Dog : Animal { }
class Cat : Animal { }

These new classes are much shorter than before and do not replicate any code. The new syntax here is a colon followed by the class name Animal, which tells C# that we want both Dog and Cat to inherit from the Animal class. In effect, Dog and Cat become child classes of Animal as illustrated in the diagram shown in Figure 14.

Figure 14: Diagram of Animal Class Inheritance

Figure 14: Diagram of Animal Class Inheritance

Note: I’ve been using the term Property so far, which is slightly inaccurate because the correct term is field as you can see in the diagram in Figure 14. Field is less clear outside the scope of programming, however, so I have used Property instead. Technically, a property is a wrapper around a field in C#.

.
We still have a problem with our Program.cs file, though, because of the custom constructor we created. If you try to run or debug our program, then you should see an error message saying that “‘PettingZoo.Cat’ does not contain a constructor that takes 1 arguments” and similar one for “PettingZoo.Dog”.

To fix this error., we need to add the constructors back in. This time, though, we’re going to utilize inheritance to inherit the constructor and show how you can extend the constructor to change the functionality of a parent class.

First, we need to create a base constructor for Animal, which will resemble the previous constructors we created earlier. Add the following code to your Animal class:

public Animal(string animalName)
{
    this.Name = animalName;
}

As before, our constructor sets the name of the animal to what we pass into it. However, this isn’t enough because we also need to add a constructor to both the Dog and Cat classes. We will use this to define which sound each animal makes too.

class Dog : Animal
{
    public Dog(string dogName) : base(dogName)
    {
        this.Sound = "Woof";
    }
}
class Cat : Animal
{
    public Cat(string catName) : base(catName)
    {
        this.Sound = "Meow";
    }
}

For both classes, we add a constructor that accepts a Name parameter. We also set this object’s Sound property to the sound we want the animal to make.

However, unlike before, we now call the parent class constructor and pass in the Name parameter from the parent constructor. This call comes through the : base() method and by adding the received dogName or catName parameters inside the parentheses.

Note: This : base() syntax is unique to constructors. We will see other ways of changing or extending case functionality later.

.
With these updates to our code, we can now run our program again and see a result similar to Figure 15.

Figure 15: Animals Speak, Using Inheritance

Figure 15: Animals Speak, Using Inheritance

Note that we have neither a Sound nor a Name property defined in either the Dog or Cat classes. We also do not we have a Speak() method. Instead, they reside in the parent Animal class, and the Dog and Cat classes inherit these properties and methods.

We can similarly create other classes now, for any type of animal and we will only need to replicate the pattern of the Dog and Cat classes. For example, we might create these classes:

class Parrot : Animal
{
    public Parrot(string parrotName)
        : base(parrotName)
    {
        this.Sound = "I want a cracker!";
    }
}
class Pig : Animal
{
    public Pig(string pigName)
        : base(pigName)
    {
        this.Sound = "Oink";
    }
}

Then, we can instantiate these classes in Program.cs:

Parrot myParrot = new Parrot("Polly");
myParrot.Speak();

Pig myPig = new Pig("Bacon");
myPig.Speak();

Now we will have a veritable zoo on our hands when we run the program, as shown in Figure 16.

Figure 16: A Zoo of Four Animals Speaking

Figure 16: A Zoo of Four Animals Speaking

You could create further sub-child classes of existing child classes, too. For example, you might separate the Dog class into Beagle and Pointer or have the Parrot class inherit from a parent Bird class that, in turn, inherits from Animal. Creating this sort of hierarchy is beyond the scope of this article, however, so let’s move on and finally look at how we can override, change, or extend the functionality of parent classes in child classes.
.

Modifying Inheritance

We can change the method of a parent class by using a technique called overriding. The syntax for overriding may differ between languages, but the principle of changing the parent’s method in the child class remains the same.

In C#, we add the keyword override followed by the method signature you want to override. We also must declare in the parent class that we are allowing children to override the methods by adding the virtual keyword to the method signature in the parent class. (Other languages may not require this additional indication.)

In our program, we need to update the Speak() method in the Animal class.

public virtual void Speak() { Console.WriteLine(this.Name + " says " + this.Sound); }

Now we can insert our override of the Speak() method in the Dog class at the beginning of our code block for this class.

class Dog : Animal
{
    public override void Speak()
    {
        base.Speak();
        Console.WriteLine("...and then runs around, chasing his tail");
    }
[remaining code omitted]

This code tells us that we want to override what happens in the parent method Speak(). We will still execute the parent method using the base.Speak() call before we output an extra line right afterwards.

Why would you want to do so? Well, perhaps we want our dogs to be as enthusiastic as they can be in person. Run the program and see for yourself:

Figure 17: Our Dog Overrides His Class Method

Figure 17: Our Dog Overrides His Class Method

First, Snoopy barks as normal: the base.Speak() method calls the Speak() method from the parent Animal class. Then the rest of the code outputs a second line to the console. Effectively, we extend the parent class by adding new functionality to the child class only. See how the Cat class is unaffected.

Of course, cats are notoriously difficult to get to do as you wish, so let’s reflect that in code. Update the Cat class and add the following method override, similar to the Dog class.

class Cat : Animal
{
    public override void Speak()
    {
        Console.WriteLine(Name + " doesn't speak but just sits there wondering when you will feed it.");
    }
[remaining code omitted]

Unlike what we did in the Dog override, we do not call the base.Speak() method this time. Without that call, we never execute the Animal Speak() method and completely change what happens in this method. See for yourself:

Figure 18: Our Cat Also Overrides His Class Method

Figure 18: Our Cat Also Overrides His Class Method

Also notice that as we have rebuilt our classes, we have not changed the Program.cs code. These changes serve as a good example of why object orientation is a very powerful technique; we have completely changed the underlying code without touching the program that uses that code. [Object orientation has allowed us to isolate many of the implementation details and simply expose a small set of interface with which the program needs to interact.]

Where We Bent the Rules

Before we wrap up, I want to point out a few things, chiefly some “bad” things we have done.

First, in our classes, we call the Console.WriteLine() method directly. This usage is not a good design decision because classes should be independent of how we output the results of the classes.

A better approach would be to return data from the classes instead and then output that as part of the program. We would then allow the program to decide what to do with the output rather than the classes, allowing us to use the same classes across many different types of programs, whether they are web pages, Windows Forms, QT, or Console applications.

Second, in our later examples, all the properties were public. Making these properties public weakens the security aspect of object orientation, where we seek to protect the data in the classes from manipulation from the outside. However, protection of properties and methods quickly becomes complex when you are dealing with inheritance so I wanted to avoid that complication, at least at this introductory stage.

Finally, there’s really no point in having Sound set as part of the child constructor at all because we were duplicating that code, too. It would be a better design to create a constructor in the parent class that accepted both a name and a sound, and then just calling that as part of the child constructor. However, again, for the sake of simplicity, I didn’t want to introduce differing constructor signatures at this stage.

So, while we’ve learned a lot in this bonus tutorial, you can see there’s still more to learn. Don’t worry too much about it just yet, however. You can take a moment to congratulate yourself on having followed along all the way. If you have followed all the steps, you have learned:

  • Why we use object orientation
  • How to create classes
  • How to create properties and methods
  • How to create constructors and what they do
  • How to take advantage of inheritance and why it’s a powerful feature of object orientation

I hope you enjoyed this extra lesson and are interested in exploring more. Be sure to check back with us for new content on the Atlantic.Net blog, and consider one our industry-leading cloud hosting servers.
.


Turning on the LAMP: The Clapper for Developers

Sam Guiliano November 10, 2015 by under Cloud Hosting 0 Comments

What is LAMP, and how can you use it to light the way of development with a simple hand motion?

  • What is LAMP?
  • Platform vs. Stack
  • Operating System – Linux
  • Webserver – Apache
  • Database – MySQL
  • Scripting Language – PHP
  • One-Click LAMP: The Clapper for Developers

Clap On! Clap Off! You may remember the jingle for the “as seen on TV” product The Clapper, a sound-activated electrical switch that captivated the imagination with its simple brilliance. Just as you can turn on a lamp with that simple hand motion of bringing your hands together, you can turn on a LAMP Hosting with the simple hand motion of clicking a mouse button.

Read More


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources