
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are Base and Derived Classes in C#?
A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.
For example, Vehicle Base class with the following Derived Classes.
Truck Bus Motobike
The derived class inherits the base class member variables and member methods.
In the same way, the derived class for Shape class can be Rectangle as in the following example.
Example
using System; namespace Program { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } // Derived class class Rectangle: Shape { public int getArea() { return (width * height); } } class Demo { static void Main(string[] args) { Rectangle Rect = new Rectangle(); Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. Console.WriteLine("Total area: {0}", Rect.getArea()); Console.ReadKey(); } } }
Output
Total area: 35
- Related Articles
- Catching base and derived classes exceptions in C++
- Virtual functions in derived classes in C++
- What are the classes in C#?
- What are collection classes in C#?
- What are nested classes in C#?
- What are abstract classes in C#?
- What are I/O classes in C#?
- Interpreter base classes in Python
- What are storage classes of variables in C++?
- Python Exception Base Classes
- Abstract Base Classes in Python (abc)
- What are JavaScript Classes and Proxies?
- What are the rules for naming classes in C#?
- What are Java classes?
- What are Java parent and child classes in Java?

Advertisements