C# Object Oriented Programming
Object Oriented Programming
Last Revision: 12th September 2018
Object Oriented Programming is a brand new(ish) way to program your project. Notice the word Object Oriented is used in the title. We create blocks (Objects) inside a Class, which can be re-used at any point of the program! Let's get cracking!
Let's say a business wanted you to create a program for them, they want to hold data on Games. They want you to make a program which helps them to organise their games for them. Let's go!
Step 1: Planning
Whenever you start a project, it's probably best to plan out how your program will work. This will (Hopefully) reduce work time as you already know what to implement. Let's go through some techniques:Abstraction
Abstraction is an OOP technique which restricts what you see and what the class sees. For example let's look at a UML Diagram:
Without Abstraction, the crazy client is able to program the same actions and functions as the program can. The client can access the Set Bank Money and gain himself a nice holiday somewhere for about.. forever. He is now as rich as Bill Gates! Of course, he can hide the evidence of it all by transferring it into a new bank and access the method Delete Bank Data. That's one sneaky boy!
Now with Abstraction, notice as the program has uni-directional arrows! The program now has complete control without this sneaky boy attempting to tamper with it! The program can now control the 3 methods, which is more secure. This is why it's important to use Abstraction to avoid these issues. The only thing the sneaky boy has access is to fetch his bank details, nice try sneaky boy!
Encapsulation
Not much to explain here, it's simple. Look at this drawings:
Polymorphism
Let's continue from the picture above:
Notice as above, each coloured block has the new same property, but is different? This is why OOP is fantastic, it's all to our fantastic number one friend, Polymorphism!
Inheritance
Let's now expand on this diagram. Inheritance is the ability to carry on characteristics from a parent class. Notice as each coloured block has the same 4 properties, we can achieve this in C# by using Interface:
Notice the name starts with the prefix I (IBank for this example) This the correct way to name these interfaces, so you can use the class name of Bank without any compiler problems, handy!
Now, we need to insert the properties, methods and functions that are in IBank into our new inherited bank class. This is a good thing as it makes sure we have the needed properties etc to make this class work!
Note: Everything has to copied EXACTLY, you cannot change the access modifier (public/private), interface doesn't support access modifiers, so by default, everything has to be public!
Now back to the planning:
Lets now find the correct data types for this program to work:
GameTitle - String
CompanyName - String
GameRelease - DateTime
GameRating - Int
Do we need Inheritance?
We don't exactly need it for this task, we would if the business was asking us to make a registrar program. We'll make a interface class called IEmployee which only holds 2 information, Name and Age. Then we can inherit it into a different class, ta-da! So we can get away with it since we're only using it for one task only.
Dynamic or Static Collection?
The business said "games" which would apply more than one, so we can eliminate Array since it's static. A List is dynamic since you don't need to initialise a size for it. So we will use a List data type to hold our game information into!
Step 2: Program/Code
The code is simple in itself, Lets code:
Notice as we use Abstraction (Notice as it's private and not public) so only the class can modify the variables, but it only makes it more difficult. How do I assign these variables if I cannot see them in my code editor? Well, introducing the Constructor!
If you are using Visual Studio, type ctor and hit tab to automatically input it for ya! Simple!
Make sure that the constructor is the same name as the class, if not, this will not work!
Using the keyword "this" tells the compiler to reference itself, this becomes handy so you can include the same name in the parameter!
Notice as using the "this" keyword, we are able to use exactly the same name! The compiler knows to reference the SameNameNumber variable, ain't that cool!
Step 3: Main Section Implementation
That's us with our custom class! We can make it FAR complex, but we'll start it easy!
PROBLEM: We cannot search for any game since all the properties are private! Functions are here to the rescue!
These are public functions, we can view these functions only. The properties are safely hidden inside the class, so we cannot accidentally modify a property! Awesome!
Final Code:
Ta-da! OOP is fantastic and easier than you think. This is only the beginning, soon we'll be experts in the next tutorial, WUH-HAHAHA *cough*