Saturday, May 31, 2014

Classes and Objects (in C#) with Example

A program is made of pieces that all interact with each other to make a functional program. Think of a human body. We have organs that each have their own function, but ideally, they will all work together to keep us alive. These small parts will be individual programs and have their own purpose and function, but to make a bigger product. Our class would be "organ". Every organ is made up of cells (attribute), and every organ uses energy (method).

So our brain is a type of organ, hence brain would be an object we created.

Brain specifically has the same property where it is made of cells and it has the method of using energy. However, it has additional properties (contains blood brain barrier) and methods (sends electrical signal using neurons, capable of interpreting thought/senses) that are SPECIFIC only to the brain. You want your class to have properties/methods that apply to almost all of the objects within it. And then you can specify more about each object when you create them.

We will start simple with some definitions to reiterate what I said above-- and then we can take the human body as an example and put it into the code (using C# as our example):

What is a class?
Classes can be regular or abstract. A regular class is a "prototype" of sorts for the objects to be created from. It will contain all of the attributes and methods that each object created from it will own (the objects will inherit the properties and methods of the class). An abstract class must declared abstract (and may/may not have abstract methods). Abstract classes cannot be instantiated. However, you can subclass an abstract class.

What is a object?
An object is an instance of a class. It will inherit the characteristics of the class (since we make an object to be a type of organ, we can make a brain. A brain is made of cells (attribute) and uses energy (method)). In addition, you can add more attributes and methods specific to the brain. And then you can make another object, like a heart.. which will have the same inheritance from the class (made of cells and uses energy), but the heart has large blood vessels (attribute), and it supplies the body with blood (method).

How do I make a new object?

You call on the class "Organ" and follow it with the name of the new object you want to make, here the new object is called "brain". "new Brain" simply creates the actual instance of the object.

This is equivalent to the statement above! Up to you on which one you prefer to write.

And if you have a bunch of brains.. well you can just name the new one 'x'.. or any other variable/name of your choice.

How do I actually connect them?
You use a colon sign to connect them, and now the brain will inherit the attributes and methods of the organ.

Other object oriented languages are C++, Objective-C, C#, Java, Perl, Python, Ruby and PHP.

We are trying to avoid being very linear and running thousands of lines of code with repeating text. Who wants to read or edit that?