Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

sealed method in c#

Prevent overriding a method of a class. This is a method that is declared with
the keyword sealed and is always used with combination of override keyword. 
Derived classes will not be able to override this method as it is sealed for
overriding.
  
Example by me: 

    class Account
    {
        public int Number { get; set; }
        public string Holder { get; set; }
        public double Balance { get; protected set; } // protected property is used to avoid the access to the balance from outside the class

        public Account()
        {

        }

        public Account (int Number, string Holder, double Balance)
        {
            this.Number = Number;
            this.Holder = Holder;
            this.Balance = Balance;
        }

        public virtual void Withdraw(double amount)
        {
            if (amount > Balance)
            {
                Console.WriteLine("The amount exceeds the balance");
            }
            else
            {
                Balance -= amount;
            }
        }

    }

	namespace TestConsoleApp.Entities.AccountProject;
    sealed class SavingsAcount : Account
    {
        public double InterestRate { get; set; }

        public SavingsAcount()
        {

        }

        public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
        {
            this.InterestRate = InterestRate;
        }

        public void UpdateBalance()
        {
            Balance += Balance * InterestRate;
        }

        public override void Withdraw(double amount)
        {
            base.Withdraw(amount);
            //UpdateBalance();
        }
    }

    sealed class SavingsAcount : Account
    {
        public double InterestRate { get; set; }

        public SavingsAcount()
        {

        }

        public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
        {
            this.InterestRate = InterestRate;
        }

        public void UpdateBalance()
        {
            Balance += Balance * InterestRate;
        }

        public sealed override void Withdraw(double amount)
        {
            base.Withdraw(amount);
            //UpdateBalance();
        }
    }

With this 2 examples, you could make a third example trying to use the 
method Widtdraw of SavingsAccount if you would pretend to derivate 
SavingAccount class but you couldn't do that.
  
Comment

sealed method in c#

Prevent overriding a method of a class. This is a method that is declared with
the keyword sealed and is always used with combination of override keyword. 
Derived classes will not be able to override this method as it is sealed for
overriding.
  
Example by me: 

    class Account
    {
        public int Number { get; set; }
        public string Holder { get; set; }
        public double Balance { get; protected set; } // protected property is used to avoid the access to the balance from outside the class

        public Account()
        {

        }

        public Account (int Number, string Holder, double Balance)
        {
            this.Number = Number;
            this.Holder = Holder;
            this.Balance = Balance;
        }

        public virtual void Withdraw(double amount)
        {
            if (amount > Balance)
            {
                Console.WriteLine("The amount exceeds the balance");
            }
            else
            {
                Balance -= amount;
            }
        }

    }

	namespace TestConsoleApp.Entities.AccountProject;
    sealed class SavingsAcount : Account
    {
        public double InterestRate { get; set; }

        public SavingsAcount()
        {

        }

        public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
        {
            this.InterestRate = InterestRate;
        }

        public void UpdateBalance()
        {
            Balance += Balance * InterestRate;
        }

        public override void Withdraw(double amount)
        {
            base.Withdraw(amount);
            //UpdateBalance();
        }
    }

    sealed class SavingsAcount : Account
    {
        public double InterestRate { get; set; }

        public SavingsAcount()
        {

        }

        public SavingsAcount(int Number, string Holder, double Balance, double InterestRate) : base(Number, Holder, Balance)
        {
            this.InterestRate = InterestRate;
        }

        public void UpdateBalance()
        {
            Balance += Balance * InterestRate;
        }

        public sealed override void Withdraw(double amount)
        {
            base.Withdraw(amount);
            //UpdateBalance();
        }
    }

With this 2 examples, you could make a third example trying to use the 
method Widtdraw of SavingsAccount if you would pretend to derivate 
SavingAccount class but you couldn't do that.
  
Comment

PREVIOUS NEXT
Code Example
Csharp :: adding to a dictionary unity 
Csharp :: hide numericUpDown arrows 
Csharp :: convert string to decimal c# 
Csharp :: c# convert date to oracle format 
Csharp :: label wpf 
Csharp :: string interpolation in c# 
Csharp :: .net on vs code 
Csharp :: c# const 
Csharp :: csharp Console.Read(); 
Csharp :: serenity frameword order column 
Csharp :: global exception handler c# 
Csharp :: sharepoint c# get list item query by lookup 
Csharp :: unity dropdown 
Csharp :: c# list keyvaluepair update value 
Csharp :: linq c# object except two lists 
Csharp :: display array elemetns to text box c# 
Csharp :: wpf change foreground c# 
Csharp :: declarar lista c# 
Csharp :: how to check if a file is running in c# 
Csharp :: c# split quotation 
Csharp :: encrypt password easiest way in web app .net 
Csharp :: c# clear linkList 
Csharp :: stroke dash array wpf 
Csharp :: w3develops 
Csharp :: print text c# unity 
Csharp :: display none asp.net 
Csharp :: c# bool list count true 
Csharp :: c# faker 
Csharp :: convert json date to datetime c# 
Csharp :: c# switch example 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =