Level 5 — Inheritance
Inheritance
Original notes by SunDiaL · View English source
Inheritance is a feature limited only to classes.
The class that is inherited from is usually called the base class or parent class and the class that inherits is usually called the child class.
Inheritance enables:
- Inheriting members from the parent class.
- This introduces new members access modifiers
- Extending the behavior of the parent class.
- Changing the behavior of the parent class. Feature known as overriding.
There is only one restriction when inheriting, and that is that you cannot inherit more than one class.
Example
Inheriting a class is done with the : (colon)
public class Parent
{
public int x;
}
public class Child : Parent
{
}
public void Main()
{
Child c = new Child();
c.x = 5; //notice how x is declared in the parent but can be used from the child
}