Here is another incredibly useful topic : Interfaces.
Start with syntax
Interfaces are ALWAYS public, we don’t have an access modifier, we declare them with keyword “interface” and the name is always starting with I and then the name : IWalkable, IAttackable, IDamagable, etc. This is for convention.
And they haven’t implementation, we declare only the body of method.
We use Interfaces because of their loosey-coupled.
Class coupling is one of the common problems when we work in large projects and with many classes and the interfaces help us a lot to solve this problem.
I use this simple example…
Abstraction is another important topic of OOP.
abstract is another keyword and we use it to define a Class without implementation.
What is means?
Back to our example : we have Enemy Class, and we have Ogre and Harpy that derive from that Class
Does it make sense for us to have an implementation of Attack() directly in the base class?
And the answer is no.
We only need the concept of Attack(), but the implementation is defines directly in Ogre or Harpy Class.
So how do we do?
We change the Enemy Class to abstract Class, and the Attack()…
What is Method Overriding?
To understand, we need to introduce two new keyword : virtual and override.
In the past article we create a Enemy Class and with Inheritance we have some different Classes derived from that.
I use same Class to make a simple example.
Enemy class with one method, Attack(), and two derived Classes : Ogre and Harpy.
Now, to use the same method Attack(), but with different implementation, we need to change our code and make the Attack() method virtual, like this
Now we talk about Inheritance, which is one of the PILLARS OF OOP.
But at same time, we talk about Composition.
In my little experience, especially at the beginning of my programming journey, I use A LOT of Inheritance and thought it was the BEST WAY to create code. Well, this is WRONG. Don’t misunderstanding, inheritance is really GREAT, but we need to watch out to something called Class Coupling.
When we create a small project, probably we don’t notice so much this type of problem.
The problem is when we try to make something little bigger, then the things start…
In this article we see what are the Properties and how work.
The main reason why we use properties, is create a getter / setter fields, but with less code!
The logic is the same as what we saw in the previous article.
I use same example I used before.
We have private field, and two methods : GetDamge and SetDamage.
Now, we change this into a property!
The syntax is this
Finally, here we are to talk about access modifier and properties. We start with access modifier.
They are : public, private, protected, internal and protected internal.
The reason why we use access modifier to make our code safe.
Let’s look at this example
I have Enemy Class, and create an instance of it.
Now, I have two fields, Name and Life, but I change them from public to private
If I go back in my Main method and try to compile
First of all, what is a Constructor?
Constructor is a method that is call when we create an instance of the Class.
Why we have to use it?
Constructor are really useful because when we make an object, we put it in “early state”, or to be clear, we can initialize fields directly in the instant we create an object.
Let’s look how work
To declare a Constructor, this is the syntax
It is a method, so the syntax is nearly the same:
we have public, then we use the SAME NAME OF CLASS, and it has no type or it…
Ok, we talked about classes in the past articles, but now I go deep in this important topic.
To declare a Class in C# we use this syntax
we start with the ACCESS MODIFIER, and it can be public, private, protected or internal.
Don’t worry, we will see everything of these topic in these articles.
Then the keyword “class” and the name of it.
The name is always in Pascal Case.
Every classes can have fields and methos
Now, let’s look what’s happen when we use the Reference type.
For example, I create an Array, because Array is a class.
Then I copy the Array on another Array, and make some modification, for example we can set the first element to 0
Ok, we are going really good.
But now we have to talk about one of the most important topic in programming : reference type and value type.
First of all : what are these “type”?
When we talk about reference type and value type, we talk about memory management. Even if C# has the Garbage Collector (in C++ we have to manage ALL), that’s don’t mean we can simply create a billion of object and everything works fine. Absolutely not! But the Garbage Collector, if we make a good work, help us a lot. …