Object Oriented Programming

Inheritance in C#

Inheritance is used to define an object with properties from another object or interface, or to inherit another object's properties.


Syntax
modifier class className : modifier superClass { 
   //methods and declarations of className 
}

Notes

If an object class inherits from another class, the subclass takes properties from the superclass (the class in which the properties are taken from).

Modifier is either public, private or protected.

Multiple inhertiance can be done, where the superclass are separated by commas (class ClassName: modifier superClass1, modifier superClass2).

The superclass should not have methods or members with the same names, but if the subclass does have a naming conflict (for the method), it can use the 'override' modifier to override the superclass method. Within this subclass override method, to refer to the superclass method, use base.methodName.


Example
class Polygon { 
   protected 
      int w, h; 
   public 
      void values (int x, int y) { 
         w=x; 
         h=y;
      } 
   }
class Rectangle : Polygon { 
   public int area () { 
      return (w * h); 
   } 
}

Add to Slack

< Abstract Methods and Classes   |   String Variables >

© 2019 SyntaxDB. All Rights Reserved.