Inheritance is the posibility of making a class act as a child of another and inherite its parent methods and properties. So if you have a class called building with the property address and you make another class called flat, you can make flat a child of building and it will inherit the property address without having to declare it.
To make flat inherit building you only need to do something like this:
public class Flat : myClasses.Building { // Here goes the class code }
Assuming that the class building is inside the namespace MyClasses.
But flat will not inherit everything building has, the private methods or properties will not be inherited. Does this means I have to declare everything as public if I want it to be inherited? No.
To solve this problem you have the protected declaration. If you make a method or property protected it will be inherited but it will not be accesible from other classes, so it will not be public, only inheritable.
public class Building { public string Address {get; set;} protected int ID {get; set;} }
Well I don’t know what to use as an example, but you get the idea.
Not only this, it may happen that you want your child class act differently as the parent class for the same action, for example imagine you have the method showProperty() that displays the building data in the screen, but a Flat has different properties than a general building and you want that method to act differently.
Then you can override a method, to make this possible you can declare a method in the parent class as virtual, and then that same method in the child class overriding the parent one.
public class Building { public virtual void ShowProperty { Console.WriteLine("Building.Show"); } } public class Flat: myClasses.Building { public override void ShowProperty { Console.WriteLine("Flat.Show"); } }
When a method has been declared as virtual it says it may be overrided by a child, but it may not. There is another way of making this, instead of overriding you can simply hide the parent property using new.
public class Building { public void ShowProperty { Console.WriteLine("Building.Show"); } } public class Flat: myClasses.Building { public new void ShowProperty { Console.WriteLine("Flat.Show"); } }
Now the child class will have its own ShowProperty() method and the parent method will not be used when you make a Flat instance.
So… What is the difference between overriding and hiding? This is a bit hard to explain… but I will try.
class HelloBase { public void HelloOne() { Console.WriteLine("Hello 1 from base"); } public virtual void HelloTwo() { Console.WriteLine("Hello 2 from base"); } } class HelloChild : HelloBase { public new void HelloOne() { Console.WriteLine("Hello 1 from child"); } public override void HelloTwo() { Console.WriteLine("Hello 2 from child"); } } class Test { static void Main() { HelloChild child = new HelloChild(); HelloBase base = child; base.HelloOne(); child.HelloOne(); base.HelloTwo(); child.HelloTwo(); } }
What will Test.Main() show?
Hello 1 from base
Hello 1 from child
Hello 2 from child
Hello 2 from child
Why is this? Well the thing is that we are making a Child object and then a Base object that points the child.
When we call to the HelloOne() method the base object uses its own method because the child has not overriden it, it has just hiden it. When we call that same method from the child the child just uses its own one.
Now when we call the HelloTwo() from base it uses the child HelloTwo() instead of its own because it has been overrided. In the end our Base object its a pointer to a Child object, so the child decides what has to be overrided or not.
This would let us, for example, show a Flat using the Flat.ShowProperty() method or the Building.ShowProperty() method. You just need to call the Flat object using a cast to make it appear as a Building:
public class Building { public void ShowProperty { Console.WriteLine("Building.Show"); } } public class Flat: myClasses.Building { public new void ShowProperty { Console.WriteLine("Flat.Show"); } } class Test { static void Main() { Flat flat = new Flat(); // Now i am going to show it as a Flat: flat.ShowProperty(); // And now as a general building: ((Building)flat).ShowProperty(); } }
What if I don’t want a class to be inherited? You only need to use the sealed modifier.
sealed class MyClass { public int x; public int y; }
Another way of making inheritance
There is another way of making a class use another class methods, which is of course making an instance of that class inside the “child” class and calling it. But this is not inheritance, it forces you to declare again all the methods and properties from the parent and doesn’t help making the code hierarchies clear.
Despite, this also means that you do not need to use inheritance for making two classes to cooperate, you will only use inherintance when it has logical sense and it helps to save implementing code.