How to use inheritance in c#
Implementing Inheritance in C#.net is similar to that in Visual Basic.net. However, there is a difference in syntax while creating base class and derived class.
Click here to know How to use inheritance in Visual Basic.Net.
Note: We cannot use the ':' symbol to derive another class in VisulBasic.Net. Instead, we have to use the 'Inherits' statement to derive the class.
The following code creates the base class in C# - Account Class
Creation of the derived class in C# - BankAccount
Click here to know How to use inheritance in Visual Basic.Net.
Note: We cannot use the ':' symbol to derive another class in VisulBasic.Net. Instead, we have to use the 'Inherits' statement to derive the class.
The following code creates the base class in C# - Account Class
class Account
{
private string mCode;
private string mName;
private string mdescription;
private double mBalance;
public Account(string code, string name, string description, double balance)
{
mCode = code;
mName = name;
mdescription = description;
mBalance = balance;
}
Creation of the derived class in C# - BankAccount
class BankAccount : Account
{
public void deposit(double amount)
{
double newBalance;
newBalance = Balance + amount;
Balance = newBalance;
}
public void withdraw(double amount)
{
double newBalance;
newBalance = Balance - amount;
Balance = newBalance;
}
}
To see how inheritance works in C#, create a windows form and name it as BankForm. Design the form as shown in the Fig below. The code for the BankForm is given below.
public partial class BankForm : Form
{
BankAccount myBankAcc = new BankAccount();
public BankForm()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
myBankAcc.Code = txtCode.Text;
myBankAcc.Name = txtName.Text;
myBankAcc.description = txtdescription.Text;
myBankAcc.Balance = Convert.ToDouble(txtBalance.Text);
}
private void btnDeposit_Click(object sender, EventArgs e)
{
myBankAcc.deposit(100);
MessageBox.Show(myBankAcc.Getbalance().ToString());
}
private void btnWithdraw_Click(object sender, EventArgs e)
{
myBankAcc.deposit(50);
MessageBox.Show(myBankAcc.Getbalance().ToString());
}
}
}
Object Oriented Programming Articles
- Abstract class constructors
- abstract class inheritance in c#.net
- abstract class inheritance in visual basic.net
- New C# Language Feature: Automatic Properties
- Calling base class constructor in C#
- base keyword in c#
- Compile time polymorphism - Method OverLoading
- How to initialize Base class Object from inside Derived Class constructor
- Constructor overloading in vb.net
- How to use inheritance in c#
- How to use inheritance in visual basic.net
- Parameter array in vb.net
- Procedure OverLoading in vb.net
- Visual Inheritance with Windows Forms
- Azure Platform
- GridView CommandField example
- GridView and DetailsView Master/Detail page using SqlDataSource control
- POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
- Query entity data model using linq to entities
- Difference between arraylist and list collection
- How to create a Web service using Visual Studio.net
- FormView DataBound Event
- Calling base class constructor in C#
- Convert a sequence to a generic list using ToList()method
- Project ideas for students
- Accounting Software
- Creating an ASP.Net MVC 3 application
- Using assemblies in .net applications
- How to implement form validation using ASP.Net 2.0 Validation Controls
- Constructors in Visual Basic.Net