-
Notifications
You must be signed in to change notification settings - Fork 5
Inheritance
Inheritance in C# allows one class to acquire the properties, methods, and behaviors of another class. This promotes code reusability, extensibility, and better organization of code.
-
Base Class (Parent)
- The class being inherited from.
- Defines shared properties and methods.
-
Derived Class (Child)
- The class inheriting from the base class.
- Can add or override functionality.
-
Access Modifiers
-
public
: Accessible to derived classes and outside the class. -
protected
: Accessible only within the class and derived classes. -
private
: Not accessible in derived classes.
-
-
Syntax
- Use the colon (
:
) to define inheritance.
- Use the colon (
- A single derived class inherits from one base class.
Example:
class Animal {
public void Eat() {
Console.WriteLine("This animal eats food.");
}
}
class Dog : Animal {
public void Bark() {
Console.WriteLine("The dog barks.");
}
}
// Usage
Dog dog = new Dog();
dog.Eat(); // Output: This animal eats food.
dog.Bark(); // Output: The dog barks.
- Multiple derived classes inherit from a single base class.
Example:
class Animal {
public void Sleep() {
Console.WriteLine("This animal sleeps.");
}
}
class Dog : Animal {
public void Bark() {
Console.WriteLine("The dog barks.");
}
}
class Cat : Animal {
public void Meow() {
Console.WriteLine("The cat meows.");
}
}
// Usage
Dog dog = new Dog();
dog.Sleep(); // Output: This animal sleeps.
dog.Bark(); // Output: The dog barks.
Cat cat = new Cat();
cat.Sleep(); // Output: This animal sleeps.
cat.Meow(); // Output: The cat meows.
- A derived class inherits from another derived class.
Example:
class Animal {
public void Breathe() {
Console.WriteLine("This animal breathes.");
}
}
class Mammal : Animal {
public void Walk() {
Console.WriteLine("This mammal walks.");
}
}
class Human : Mammal {
public void Speak() {
Console.WriteLine("Humans can speak.");
}
}
// Usage
Human human = new Human();
human.Breathe(); // Output: This animal breathes.
human.Walk(); // Output: This mammal walks.
human.Speak(); // Output: Humans can speak.
- Not supported directly in C# to avoid ambiguity.
- Achieved using interfaces.
Example (using interfaces):
interface IAnimal {
void Eat();
}
interface IWalker {
void Walk();
}
class Human : IAnimal, IWalker {
public void Eat() {
Console.WriteLine("Humans eat food.");
}
public void Walk() {
Console.WriteLine("Humans walk upright.");
}
}
// Usage
Human human = new Human();
human.Eat(); // Output: Humans eat food.
human.Walk(); // Output: Humans walk upright.
Modifier | Base Class Accessibility | Derived Class Accessibility |
---|---|---|
public |
Accessible everywhere. | Accessible everywhere. |
protected |
Not accessible outside. | Accessible in derived classes. |
private |
Only accessible within class. | Not accessible in derived class. |
- Virtual methods in the base class can be overridden in the derived class.
Example:
class Animal {
public virtual void Sound() {
Console.WriteLine("This animal makes a sound.");
}
}
class Dog : Animal {
public override void Sound() {
Console.WriteLine("The dog barks.");
}
}
// Usage
Dog dog = new Dog();
dog.Sound(); // Output: The dog barks.
- Prevent further overriding in derived classes.
Example:
class Animal {
public virtual void Sound() {
Console.WriteLine("This animal makes a sound.");
}
}
class Dog : Animal {
public sealed override void Sound() {
Console.WriteLine("The dog barks.");
}
}
Abstraction in C# allows you to hide the implementation details and show only the essential features of an object. It helps in reducing complexity and allows you to focus on what an object does instead of how it does it.
- Abstract Class: Cannot be instantiated and may contain abstract methods that must be implemented by derived classes.
- Abstract Method: A method without a body that must be overridden in derived classes.
Example:
abstract class Animal {
public abstract void Sound();
public void Sleep() {
Console.WriteLine("This animal sleeps.");
}
}
class Dog : Animal {
public override void Sound() {
Console.WriteLine("The dog barks.");
}
}
// Usage
Dog dog = new Dog();
dog.Sound(); // Output: The dog barks.
dog.Sleep(); // Output: This animal sleeps.
- Interface: Defines a contract that implementing classes must fulfill. Interfaces can contain methods, properties, events, and indexers but no implementation.
Example:
interface IAnimal {
void Sound();
}
class Dog : IAnimal {
public void Sound() {
Console.WriteLine("The dog barks.");
}
}
// Usage
Dog dog = new Dog();
dog.Sound(); // Output: The dog barks.
- Abstraction: Hides complex implementation details and shows only the necessary features.
- Abstract Classes: Define methods to be implemented by derived classes.
- Interfaces: Define a contract for classes to implement.
Abstraction helps in managing complexity and focusing on the relevant aspects of an object, making your code more modular and easier to maintain. Combining inheritance and abstraction can lead to highly reusable and flexible code structures.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.