
- 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
C# and Multiple Inheritance
Multiple Inheritance isn’t supported in C#. To implement multiple inheritances, use Interfaces.
Here is our interface PaintCost in class Shape −
public interface PaintCost { int getCost(int area); }
The shape is our base class whereas Rectangle is the derived class −
class Rectangle : Shape, PaintCost { public int getArea() { return (width * height); } public int getCost(int area) { return area * 80; } }
Let us now see the complete code to implement Interfaces for multiple inheritances in C# −
Using System; namespace MyInheritance { class Shape { public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } protected int width; protected int height; } public interface PaintCost { int getCost(int area); } class Rectangle : Shape, PaintCost { public int getArea() { return (width * height); } public int getCost(int area) { return area * 80; } } class RectangleDemo { static void Main(string[] args) { Rectangle Rect = new Rectangle(); int area; Rect.setWidth(8); Rect.setHeight(10); area = Rect.getArea(); // Print the area of the object. Console.WriteLine("Total area: {0}", Rect.getArea()); Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area)); Console.ReadKey(); } } }
- Related Articles
- Java and multiple inheritance
- Multiple Inheritance in C++
- Multiple inheritance in JavaScript
- Does Python support multiple inheritance?
- Multiple inheritance by Interface in Java
- Java Program to Implement Multiple Inheritance
- Haskell Program to Implement multiple inheritance
- Why multiple inheritance is not supported by Java?
- Why multiple inheritance is not supported in Java
- How multiple inheritance is implemented using interfaces in Java?
- How we can extend multiple Python classes in inheritance?
- How does Python's super() work with multiple inheritance?
- Does Java support multiple inheritance? Why? How can we resolve this?
- What is diamond problem in case of multiple inheritance in java?
- Subclasses, Superclasses, and Inheritance

Advertisements