<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sudha Reddy</title>
    <description>The latest articles on DEV Community by Sudha Reddy (@sudha_reddy_0608).</description>
    <link>https://dev.to/sudha_reddy_0608</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3093086%2F783f669c-c0b8-4dcb-9ee4-773261097062.jpeg</url>
      <title>DEV Community: Sudha Reddy</title>
      <link>https://dev.to/sudha_reddy_0608</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sudha_reddy_0608"/>
    <language>en</language>
    <item>
      <title>Understanding SOLID Violations in a Class (With Example and Solution)</title>
      <dc:creator>Sudha Reddy</dc:creator>
      <pubDate>Sun, 27 Apr 2025 18:23:25 +0000</pubDate>
      <link>https://dev.to/sudha_reddy_0608/understanding-solid-violations-in-a-class-with-example-and-solution-44i3</link>
      <guid>https://dev.to/sudha_reddy_0608/understanding-solid-violations-in-a-class-with-example-and-solution-44i3</guid>
      <description>&lt;p&gt;What issues do you see in the below code? Which SOLID principles are being violated, and how would you correct them?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Account
{
    public decimal balance { get; set; }
    public decimal CalculateInterest(string accountType)
    {
        decimal interest = 0;
        if (accountType.Equals("Regular"))
        {
            interest = balance * 0.4m;
            if (balance &amp;lt; 1000)
                interest -= balance * 0.2m;
            else if (balance &amp;lt; 50000)
                interest += balance * 0.4m;
        }
        else if (accountType.Equals("Salary"))
        {
            interest = balance * 0.5m;
        }
        return interest;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problems in the Code
&lt;/h2&gt;

&lt;p&gt;• SRP Violated: Single Responsibility Principle&lt;br&gt;
• OCP Violated: Open/Closed Principle&lt;br&gt;
(Other SOLID principles are not applicable yet.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why is SRP Violated?&lt;/strong&gt;&lt;br&gt;
• Account class handles:&lt;br&gt;
     Balance data&lt;br&gt;
     Interest calculation logic&lt;br&gt;
• Two reasons to change = SRP violation.&lt;br&gt;
SRP says: A class should have only one reason to change.&lt;br&gt;
&lt;strong&gt;Why is OCP Violated?&lt;/strong&gt;&lt;br&gt;
• Adding new account types (e.g., "Premium") needs modifying CalculateInterest.&lt;br&gt;
• Risk of introducing bugs while changing old code.&lt;br&gt;
OCP says:&lt;br&gt;
• Open for extension&lt;br&gt;
• Closed for modification&lt;/p&gt;
&lt;h2&gt;
  
  
  Correct Approach:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Using Polymorphism (Inheritance)&lt;/strong&gt;&lt;br&gt;
• Create an abstract base class Account.&lt;br&gt;
• Subclasses like RegularAccount, SalaryAccount override CalculateInterest.&lt;br&gt;
• Follows SRP and OCP.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public abstract class Account
{
    public decimal Balance { get; set; }
    public abstract decimal CalculateInterest();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class RegularAccount : Account
{
    public override decimal CalculateInterest()
    {
        decimal interest = Balance * 0.4m;
        if (Balance &amp;lt; 1000)
            interest -= Balance * 0.2m;
        else if (Balance &amp;lt; 50000)
            interest += Balance * 0.4m;
        return interest;
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SalaryAccount : Account
{
    public override decimal CalculateInterest()
    {
        return Balance * 0.5m;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;using Strategy Pattern we can correct this code&lt;/strong&gt;&lt;br&gt;
• Create an IInterestCalculator interface.&lt;br&gt;
• Create separate classes for each account type.&lt;br&gt;
• Account class delegates calculation to the correct strategy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface IInterestCalculator
{
    decimal CalculateInterest(decimal balance);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class RegularAccountInterestCalculator : IInterestCalculator
{
    public decimal CalculateInterest(decimal balance)
    {
        decimal interest = balance * 0.4m;
        if (balance &amp;lt; 1000)
            interest -= balance * 0.2m;
        else if (balance &amp;lt; 50000)
            interest += balance * 0.4m;
        return interest;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SalaryAccountInterestCalculator : IInterestCalculator
{
    public decimal CalculateInterest(decimal balance)
    {
        return balance * 0.5m;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modified Account Class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Account
{
    public decimal Balance { get; set; }
    private readonly IInterestCalculator _interestCalculator;

    public Account(IInterestCalculator interestCalculator)
    {
        _interestCalculator = interestCalculator;
    }

    public decimal CalculateInterest()
    {
        return _interestCalculator.CalculateInterest(Balance);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Final Benefits&lt;br&gt;
• SRP Achieved:&lt;br&gt;
   Account only manages balance.&lt;br&gt;
   Separate classes manage interest logic.&lt;br&gt;
• OCP Achieved:&lt;br&gt;
   New account types? Just add a new class!&lt;br&gt;
• Code is maintainable, testable, and professional.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>csharp</category>
      <category>solidprinciples</category>
      <category>oop</category>
    </item>
    <item>
      <title>Inheritance in C# – A Practical Guide with Example</title>
      <dc:creator>Sudha Reddy</dc:creator>
      <pubDate>Sat, 26 Apr 2025 12:09:53 +0000</pubDate>
      <link>https://dev.to/sudha_reddy_0608/inheritance-in-c-a-practical-guide-with-example-13h9</link>
      <guid>https://dev.to/sudha_reddy_0608/inheritance-in-c-a-practical-guide-with-example-13h9</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Inheritance?&lt;/strong&gt;&lt;br&gt;
In C#, inheritance allows a derived class to access the properties and methods of a base class. It enables code reuse, extension, and customization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Concepts&lt;/strong&gt;&lt;br&gt;
•Single Inheritance: In C#, Multiple inheritance is not possible, a class can inherit only from one base class.&lt;br&gt;
• Access Levels: Child classes can access public and protected members of the parent class.&lt;br&gt;
• Method Overriding: Derived classes can override base class methods to change the base class method behaviour.&lt;br&gt;
• Syntax: Use the : symbol to inherit from a base class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Account and SavingsAccount&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Base Class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Account
{
    public string AccountNumber { get; set; }
    public double Balance { get; set; }
    public void Deposit(double amount)
    {
        Balance += amount;
        Console.WriteLine($"Deposited {amount}. New Balance: {Balance}");
    }
    public void Withdraw(double amount)
    {
        if (Balance &amp;gt;= amount)
        {
            Balance -= amount;
            Console.WriteLine($"Withdrawn {amount}. Remaining Balance: {Balance}");
        }
        else
        {
            Console.WriteLine("Insufficient funds.");
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Derived Class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SavingsAccount : Account
{
    public double InterestRate { get; set; }
    public void ApplyInterest()
    {
        double interest = Balance * InterestRate / 100;
        Balance += interest;
        Console.WriteLine($"Interest applied: {interest}. New Balance: {Balance}");
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Program Execution&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Program
{
    static void Main(string[] args)
    {
        SavingsAccount savings = new SavingsAccount();
        savings.AccountNumber = "SA12345";
        savings.Balance = 1000;
        savings.InterestRate = 5;

        savings.Deposit(500);
        savings.Withdraw(200);
        savings.ApplyInterest();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
Deposited 500. New Balance: 1500&lt;br&gt;
Withdrawn 200. Remaining Balance: 1300&lt;br&gt;
Interest applied: 65. New Balance: 1365&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why To  Use Inheritance?&lt;/strong&gt;&lt;br&gt;
• Code Reuse: Common operations like Deposit and Withdraw are defined once in Account.&lt;br&gt;
• Extensibility: SavingsAccount adds new features (like interest calculation) without rewriting everything.&lt;br&gt;
• Cleaner Architecture: Helps organize related classes in a logical structure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Points to Remember&lt;/strong&gt;&lt;br&gt;
• Constructors are not inherited but can be called using base().&lt;br&gt;
• Private members of a base class are not accessible in the derived class.&lt;br&gt;
• You can override virtual methods to customize behaviour.&lt;br&gt;
• Sealed classes cannot be inherited.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advanced Example: Method Overriding&lt;/strong&gt;&lt;br&gt;
Suppose we want a specialized Withdraw behaviour for SavingsAccount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Account
{
    public virtual void Withdraw(double amount)
    {
        Console.WriteLine($"Withdrawing {amount} from Account.");
    }
}

public class SavingsAccount : Account
{
    public override void Withdraw(double amount)
    {
        Console.WriteLine($"Withdrawing {amount} from SavingsAccount with extra checks.");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, calling Withdraw() on a SavingsAccount will execute the overridden method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
Inheritance is a fundamental principle of Object-Oriented Programming in C#. By enabling the reuse and extension of base class functionality, it promotes the development of clean, maintainable, and scalable applications. A strong grasp of inheritance concepts significantly enhances your ability to design robust and efficient software within the .NET ecosystem.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>csharp</category>
      <category>inheritance</category>
      <category>oop</category>
    </item>
  </channel>
</rss>
