
- 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
Inheritance vs Composition in C#
Inheritance
With Inheritance, you can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class. Inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.
For example, A base class Shape has a derived classes like Circle, Square, Rectangle, etc.
Composition
Under Composition, if the parent object is deleted, then the child object also loses its status. Composition is a special type of Aggregation and gives a part-of relationship.
For example, A Car has an engine. If the car is destroyed, the engine is destroyed as well.
Example
public class Engine { . . . } public class Car { Engine eng = new Engine(); ....... }
- Related Articles
- Composition vs inheritance in React.js
- Difference between inheritance and composition in Java
- Composition vs Aggregation in C#
- Inheritance in C++ vs Java
- Delegation vs Inheritance in C#
- inheritance(is-a) v/s composition (has-a) relationship in Java
- Explain Inheritance vs Instantiation for Python classes.
- In JavaScript inheritance how to differentiate Object.create vs new?
- Inheritance in Python
- Inheritance in Java
- Composition attribute in HTML5 Canvas?
- What is composition in C#?
- Multiple Inheritance in C++
- Multiple inheritance in JavaScript
- Class Inheritance in Python

Advertisements