Index
- Fundamentals of Classes
- Encapsulation
- Inheritance
- Polymorphism
1. Fundamentals of Classes
First is important to talk about, what is object oriented program? and what other types of programming types are?
In the past was very common to create applications filled with methods that cover different functionalities and all the code was executed sequentially. At some point, the technology advanced and we are able to use object oriented programming in our apps, which allow us to model our applications in a more closer way to real life. Every class defines an object from the real world with methods and properties which define that object.
A class is like a blueprint for a house, a person, an animal... It's just the definition of the object we'd like to model in our application and for the animal example, it contains the type of animal, the weight, the colour, etc. But also the different behaviours derived from that object in the real life, for instance, our Animal class would contain methods to make noise, to move... At some point when you have finished writing your class, when you run your application, that class will be created within the computer's dynamic memory and it became an "object". We call that process: instantiate.
2. Encapsulation
By using encapsulation, the purpose is to create all the data and behaviours within our class, to provide that information to the user/developer who uses our class but in a way that user doesn't have access to all the components in the class just those ones we want to make visible. It'd look like a black box. For instance, the user can't set the values directly into our variables, the idea of encapsulation is to provide a bunch of properties where set that values if we want as owners of that class.
Let's take a look to one example. In this class we have created a set of properties (data) and methods (behaviour) related with any animal.
class Animal { // member variables (attributes) private string type; private double weight; private string color; // properties public string Type { get { return type; } set { type = value; } } public double Weight { get { return weight; } set { weight = value; } } public string Color { get { return color; } set { color = value; } } // instance methods public virtual void MakeNoise() { Console.WriteLine("Animal is making noise"); } public void Move() { Console.WriteLine("Animal is moving"); } }
As you can see there are some variables like type, weight,... but they are set as private, that means, we can only change the value for those properties inside of the class, we cannot save a value from other class (encapsulation). In order to change the value of our variables, we've created some public properties and inside of them we've set-up a "set" and "get" method which allow us to have a unique place to work with this variables from outside of our class in order to get and set the value we want. When a set or get from a property is raised we are actually pointing to the private variables in the top of our class. We also can define this properties as "readonly" avoiding the possiblity of stablish the value for a certain property.
At the end of our class you can see some public methods related with a normal Animal. Don't worry too much about the syntax, just try to focus on the meaning of those methods. When some of then is raised the instanced object of our Animal class will print on the console some text to know the method was executed.
3. Inheritance
As we already mentioned before, the main purpose of object oriented programming is model objects from real life, and inheritance has a main role at this point. It allows us to define certain properties and methods just once and then, other classes can inherit from them. When we inherit from another class we are getting his data and behaviours. We call super to the class that plays the father role and sub to the son class. It provides base functionality for similar objects and it helps developers to reuse code.
In our example, "MakeNoise" and "Move" are methods quite abstract because they are shared by all the animals in the world. Imagine we create a Dog class which inherits (sub) from our Animal (super) class, then, we will be able to access all the information in our father (properties and methods) and even change it to align with the way a dog moves or makes noise. To do this you just need to add the name of the father class on the right of your class name like here: public class Dog : Animal
At this point we have an empty Dog class but just with specifying the father class we will be able to access all the methods and properties from the Dog class. In the following example you can see an empty Dog class but when we instantiate that class we can get some methods and properties. They are still placed into the Animal class but throw inheritance we are able to bridge that info.
In our example, "MakeNoise" and "Move" are methods quite abstract because they are shared by all the animals in the world. Imagine we create a Dog class which inherits (sub) from our Animal (super) class, then, we will be able to access all the information in our father (properties and methods) and even change it to align with the way a dog moves or makes noise. To do this you just need to add the name of the father class on the right of your class name like here: public class Dog : Animal
At this point we have an empty Dog class but just with specifying the father class we will be able to access all the methods and properties from the Dog class. In the following example you can see an empty Dog class but when we instantiate that class we can get some methods and properties. They are still placed into the Animal class but throw inheritance we are able to bridge that info.
// Empty class inherits from Animal class Dog : Animal { } class Program { static void Main(string[] args) { // Tobby is our object from the Dog class. Dog Tobby = new Dog(); Tobby.Color = "black"; Tobby.Weight = 45; Tobby.MakeNoise(); } }
4. Polymorphism
What this rare word means is: change the behaviour of our classes. Is very useful when you work with inheritance because it allows the developers to modify the methods from the base class. For instance you can define new properties within the Dog class which not exist within our Animal class like "breed". Breed is a feature for a dog but not for an animal. For our example, in the Dog class, we would be able to override the "MakeNoise" method from the Animal class into something more specific for a dog: Console.WriteLine("Bark!!!"); To be able to do that you need to define your method as virtual within the father class and override it in the sub class. Take a look at the code below and the output of our application:
class Dog : Animal { // attributes private string breed; // properties public string Breed { get { return breed; } set { breed = value; } } // We add a new method for our Dog which cannot exist in the Animal class public void WagTail() { Console.WriteLine("Dog wags tail."); } // Here we are changing the functionality of our MakeNoise() method. public override void MakeNoise() { Console.WriteLine("Bark!!!"); } } class Program { static void Main(string[] args) { Dog Tobby = new Dog(); Animal myAnimal = new Animal(); Tobby.MakeNoise(); myAnimal.MakeNoise(); // OUTPUT // Bark!!! // Animal is making noise } }
Maybe this example is not very useful for an enterprise environment but imagine you want to create a new ERP for your company and first thing you want to do is model the different employees that are working currently.
You can create a first a class called Employee which contains the basic data for every employee like: name, surname, telephone, ... and also functionalities like getDOB, getName, getPhone, getSalary. Then you can create new classes which inherit from your Employee like HhrrMen, SalesMen, Manager ... and within this classes you can add more information related with a salesman, for example, like: bonus, objectives, more functionalities with new methods like: getOffer, getBudget and finally override some methods (polymorphism) from the base class if you want to change their behaviour, you can change the getSalary method to also print the bonuses of your salesman.
...and that's it, I hope you enjoy and don't hesitate to leave a comment with your thoughts, Cheers!
0 comments:
Post a Comment