0% found this document useful (0 votes)
11 views5 pages

Inheritance

Inheritance is a key concept in object-oriented programming that allows new classes to be created based on existing ones, promoting code reuse and establishing a hierarchical relationship. It involves base classes (parent) and derived classes (child), with features such as access modifiers, virtual and override keywords, and constructors. C# supports single inheritance and provides mechanisms like sealed classes and methods to control inheritance behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Inheritance

Inheritance is a key concept in object-oriented programming that allows new classes to be created based on existing ones, promoting code reuse and establishing a hierarchical relationship. It involves base classes (parent) and derived classes (child), with features such as access modifiers, virtual and override keywords, and constructors. C# supports single inheritance and provides mechanisms like sealed classes and methods to control inheritance behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Object Oriented Programming

Inheritance

Inheritance is a fundamental concept of object-oriented programming, allowing you to create new


classes based on existing ones. Inheritance is one of the four pillars of object-oriented
programming (along with encapsulation, abstraction, and polymorphism). It allows a class to
acquire the properties and behaviors of another class, promoting code reuse and establishing a
hierarchical relationship between classes. Similar to like a family tree where children inherit from
their parents. In the programming world, this "inheritance" means that a derived class (the child)
gains the properties (fields) and behaviors (methods) of its base class (the parent). This promotes
code reusability and helps in building a more organized and maintainable codebase.

// Base class

public class Animal

{ // Properties, methods, events }

// Derived class public class Dog : Animal

{ // Additional properties, methods, overrides }

Core Concepts of Inheritance

1. Base Class (Parent Class): This is the class whose members are inherited. It defines the
common characteristics and behaviors that its derived classes will share.
2. Derived Class (Child Class): This is the class that inherits from the base class. It can add
new members (fields and methods), override the inherited members to provide a
specialized implementation, or simply use the inherited members as they are.
3. : Syntax for Inheritance: In C#, you use a colon (:) followed by the name of the base
class when declaring a derived class.

C#

public class DerivedClass : BaseClass


{
// Members of the derived class
}

4. protected Access Modifier: Members declared as protected in a base class are


accessible within the base class itself and in any of its derived classes. They are not directly
accessible from outside the class hierarchy. This is often used to provide access to members
that derived classes might need while still restricting external access.
5. virtual and override Keywords:
o The virtual keyword is used in the base class to declare a method or property
that can be overridden (reimplemented) in a derived class.
o The override keyword is used in the derived class to provide a specific
implementation for a virtual member inherited from the base class.
6. base Keyword: Within a derived class, the base keyword is used to access members of
the base class. This is particularly useful when you override a method but still want to call
the base class's implementation. It's also used to call the constructor of the base class from
the derived class's constructor.
7. Constructors in Inheritance: When you create an instance of a derived class, the
constructor of the base class is executed first (implicitly or explicitly using base(...)).
This ensures that the base class is properly initialized before the derived class's
initialization takes place.
8. Single Inheritance: C# supports single inheritance, meaning a class can inherit directly
from only one base class. However, you can achieve a form of multiple inheritance through
interfaces.

Code Example: A Simple Windows Forms Application

A simple application to display information about different types of shapes.

2. Add a New Class Named Shape.cs:

using System;
namespace ShapeApp
{ public class Shape
{
public string Color;
public Shape(string assign_color)
{ Color = assign_color; }

public virtual double Area()


{
return 0; // Base class area is 0 as it's a generic shape
}
public virtual string Display()
{
return "Color:" + Color;
}
}
}
In this Shape class:

• There is a Color property.


• The constructor initializes the Color using the parameter assign_color.
• The Area() method is declared as virtual, meaning derived classes can override it to
calculate their specific area. The base implementation returns 0 as a generic shape doesn't
have a specific area formula.
• The Display() method is also virtual and provides a basic way to display shape
information.

// Overriding base class (Shape) methods

using System;
namespace ShapeApp
{
public class Circle : Shape
{
public double Radius;
public Circle(string assign_color, double radius)
{
Radius = radius;
}
public override double Area()
{
return Math.PI * Math.Pow(Radius, 2);
}
public override string Display()
{
return base.Display() + $", Shape: Circle, Radius:
{Radius}, Area: {Area():F2}";
}
}
}

Now here:

• The Circle class is inheriting the Shape class.


• The Circle class has a Radius property and a constructor that takes color and radius
parameters.
• The Area method is overridden to calculate the area of the circle using the formula πr2.
• The Display method is overridden to include the shape type, radius, and area. The
base.Display() call includes the color information from the base class.
You can use this class as
public class Program
{
public static void Main()
{
Circle my_circle = new Circle("Red", 5);
MessageBox.Show(my_circle.Display());
}
}

Key points of Inheritance in C#

1. Inheritance allows code reuse: A derived class can inherit properties, methods, and behavior
from a base class.
2. Base class and derived class: The base class is the parent class, and the derived class is the
child class that inherits from the base class.
3. Single inheritance: C# supports single inheritance, where a derived class can inherit from
only one base class.
4. Multilevel inheritance: A derived class can inherit from a base class that itself inherits from
another base class. e.g.
class A{ } class B:A { } class C:B { }
here class C inherits from class B and class A (through B).
5. Access modifiers: Access modifiers (public, protected, internal, private) control access to
members of a class.
6. Virtual and override: Virtual methods in the base class can be overridden in the derived class
using the override keyword.
7. Sealed classes and methods: Sealed classes cannot be inherited from, and sealed methods
cannot be overridden.
8. Abstract classes and methods: Abstract classes cannot be instantiated and are intended to be
inherited from. Abstract methods are declared without implementation and must be
implemented in derived classes.
2. Access Modifiers and Inheritance

When inheriting, members of the base class are accessible based on their access modifiers:

• public members are accessible anywhere


• protected members are accessible within the base class and derived classes
• private members are accessible only within the same class they are declared
• internal members are accessible within the same assembly*
• protected internal members are accessible within the same assembly or derived
classes.

* An assembly is an executable (.exe) or dynamic link library (.dll) produced from compiling
source code.

Sealed Keyword:
The sealed keyword in C# is used to restrict inheritance or overriding.

Sealed Class: A sealed class cannot be inherited from. It cannot be used as base class.
public sealed class Rectangle
{
// class members
}
Sealed Method: A sealed method cannot be overridden in derived classes. Unlike private which
is not inherited, sealed is inherited in derived class but it cannot be overridden.
class BaseClass
{
public virtual void MyMethod() {
Console.WriteLine("Base Method"); }
}

class DerivedClass : BaseClass


{
public override sealed void MyMethod() {
Console.WriteLine("Derived Method"); } // Sealed method
}

class AnotherDerivedClass : DerivedClass


{ //public override void MyMethod() { //Error: Cannot override
sealed method }
}
The “sealed” keyword in “DerivedClass” prevents further overriding of the `MyMethod` in any
class that inherits from it, as shown in `AnotherDerivedClass`.

You might also like