C# Inheritance

Inheritance is one of the most characters of classes.

class phone
{
public double weight;
public string make;
public phone(string m) {make=m;}
public string dial() {return "keyboard";}
public string imake
{
get
{
return make;
}
set
{
make = value;
}
}
}
class smartphone : phone
{
double screenwidth;
double screenheight;
public string dial() {return "touch screen";}
public smartphone(string m,double w, double h) : base(m)
{make=m;screenwidth=w;screenheight=h;}
}

The inherited class inherited all methods, properties of the base class.
smartphone sp = new smartphone("Apple",6.2,10);
sp.imake = "Samsung";

The method in the inherited class will hide the method with the same method name in the base.
sp.dial(); //touch screen

If you do not want the class to be inherited by other class, you can use sealed keyword. Sealed class or method can not be inherited or overrided.
sealed class tv
{...}
class smartphone : tv {...} //error

Inherit two classes:
class camera
{...}
class smartphone : phone, camera
{
...
}



endmemo.com © 2024  | Terms of Use | Privacy | Home