Can you explain what inheritance is and an example of when
you might use it?

Answers were Sorted based on User's Feedback



Can you explain what inheritance is and an example of when you might use it?..

Answer / sandeep

In Inheritance we can use one class property into another
class..
using System;
class sample
{
public void display()
{
Console.WriteLine("C#");
}
}
class sample1:sample
//(Inheriting the property of class sample in class sample1)
{
public void disp()
{
Console.WriteLine("C++");
}
}
class Test
{
public static void Main()
{
sample1 sm=new sample1(); //creating a object of sample1
sm.display(); //accessing function of sample class
sm.disp();
}
i think dear u got ur write answer..

Is This Answer Correct ?    4 Yes 0 No

Can you explain what inheritance is and an example of when you might use it?..

Answer / ramgopal reddy

When you want to inherit (use the functionality of) another
class. Base Class Employee. A Manager class could be derived
from the Employee base class.

Is This Answer Correct ?    2 Yes 0 No

Can you explain what inheritance is and an example of when you might use it?..

Answer / shikhar

a class which contains all the
properties,methods,variables of its parent class .., and
this method of adopting from any class is called
inheritance....

Is This Answer Correct ?    1 Yes 1 No

Can you explain what inheritance is and an example of when you might use it?..

Answer / ajay vikram

The savingaccount class has two data members-accno that stores account number, and trans that keeps track of the number of transactions. We can create an object of savingaccount class as shown below.

savingaccount s = new savingaccount ( "Amar", 5600.00f ) ;

From the constructor of savingaccount class we have called the two-argument constructor of the account class using the base keyword and passed the name and balance to this constructor using which the data member's name and balance are initialised.

We can write our own definition of a method that already exists in a base class. This is called method overriding. We have overridden the deposit( ) and withdraw( ) methods in the savingaccount class so that we can make sure that each account maintains a minimum balance of Rs. 500 and the total number of transactions do not exceed 10. From these methods we have called the base class's methods to update the balance using the base keyword. We have also overridden the display( ) method to display additional information, i.e. account number.

Working of currentaccount class is more or less similar to that of savingaccount class.
Using the derived class's object, if we call a method that is not overridden in the derived class, the base class method gets executed. Using derived class's object we can call base class's methods, but the reverse is not allowed.

Unlike C++, C# does not support multiple inheritance. So, in C# every class has exactly one base class.
Now, suppose we declare reference to the base class and store in it the address of instance of derived class as shown below.
account a1 = new savingaccount ( "Amar", 5600.00f ) ;
account a2 = new currentaccount ( "MyCompany Pvt. Ltd.", 126000.00f) ;
Such a situation arises when we have to decide at run-time a method of which class in a class hierarchy should get called. Using a1 and a2, suppose we call the method display( ), ideally the method of derived class should get called. But it is the method of base class that gets called. This is because the compiler considers the type of reference (account in this case) and resolves the method call. So, to call the proper method we must make a small change in our program. We must use the virtual keyword while defining the methods in base class as shown below.
public virtual void display( ) { }
We must declare the methods as virtual if they are going to be overridden in derived class. To override a virtual method in derived classes we must use the override keyword as given below.
public override void display( ) { }
Now it is ensured that when we call the methods using upcasted reference, it is the derived class's method that would get called. Actually, when we declare a virtual method, while calling it, the compiler considers the contents of the reference rather than its type.
If we don't want to override base class's virtual method, we can declare it with new modifier in derived class. The new modifier indicates that the method is new to this class and is not an override of a base class method.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More ASP.NET Interview Questions

What?s the difference between Codebehind="MyCode.aspx.cs" and Src="MyCode.aspx.cs"?

1 Answers   Getit, Siebel, Visual Soft,


Which of the following hosts Can't be used to create .net Application? a) IIS b) Internet Explorer c) ASP.Net d) Windows Shell Which one is correct?

1 Answers   CTS, TCS,


What is the purpose of the following segment? If ( !IsPostBack) { sqldataAdapter1.Fill (dsusers1); DataGrid1.DataBind (); } a) To populate the DataAdapter the first time the web page id displayed. b) To populate the DataSet every time the web page is displayed. c) To populate the DataAdapter every time the web page is displayed. d)To populate the DataSet the first time the web page is displayed.

2 Answers   Syntax Softtech,


What is state management in .net?

0 Answers  


Differentiate globalization and localization.

0 Answers  






What is x xss protection?

0 Answers  


Define page output caching?

0 Answers  


what is Impersonation

3 Answers   Alliance One, Task Informatics,


What is the need to give <compilation debug=true> command ?

3 Answers   Netsweeper,


What is meant by role based security? when we use this one

2 Answers  


how can you bind data from dataset to XML file

2 Answers   LG Soft,


What is the advantage of mvc over asp.net? : Asp.Net MVC

0 Answers  


Categories