How to call a non virtual function in the derived class by
using base class pointer

Answer Posted / annu agrawal

This can be done by using 'base' keyword in C# language. For eg;

An abstract class is coded as follows:

abstract class AbstractDemo
{
public abstract void Habits();

public virtual void hello()
{
Console.WriteLine("Hello DerievedAbstract class,
hello()");
}

public void adddet()
{
Console.WriteLine("Hello Everybody...");
}
}

Then, I have created a derieved class of this class as:

class DerievedAbstract : AbstractDemo
{
public override void Habits()
{
Console.WriteLine("Hello DerievedAbstract class");
}

public override void hello()
{
base.adddet();
Console.WriteLine("Hello() is a function....");
}

new public void adddet()
{
Console.WriteLine("Hello Everybody....derieved
class");
}
}

In the Main() function, the object of Derieved class is
created as:

class classmain
{
public static void Main(string[] args)
{
DerievedAbstract ab = new DerievedAbstract();
ab.Habits();
ab.hello();
Console.ReadLine();
}
}

Is This Answer Correct ?    1 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why multiple inheritance is not possible?

590


What is the difference between encapsulation and polymorphism?

582


What is the example of polymorphism?

552


What makes a language oop?

590


What are classes oop?

593






INSTANCE FIELDS DECLARED private ARE ACCESSIBLE BY THE METHODS ONLY.CAN WE CHANGE THE private FIELD OF AN OBJECT IN A METHOD OF SOME OTHER OBJECT OF THE SAME CLASS?

1628


Do you know about multiple inheritance?

634


What polymorphism means?

614


Question In a class, there is a reference or pointer of an object of another class embedded, and the memory is either allocated or assigned to the new object created for this class. In the constructor, parameters are passed to initialize the data members and the embedded object reference to get inialized. What measures or design change should be advised for proper destruction and avioding memory leaks, getting pointers dangling for the embedded object memory allocation? Please suggest. Question Submitted By :: Sunil Kumar I also faced this Question!! Rank Answer Posted By Re: In a class, there is a reference or pointer of an object of another class embedded, and the memory is either allocated or assigned to the new object created for this class. In the constructor, parameters are passed to initialize the data members and the embedded object reference to get inialized. What measures or design change should be advised for proper destruction and avioding memory leaks, getting pointers dangling for the embedded object memory allocation? Please suggest. Answer # 1 use copy constructors 0 Shanthila There is something to be taken care in destructor, in copy constructor, suppose the memory is assigned to the embedded member object pointer with the parameter passed value, but if some other objects of different class also are pointing to this memory, then if some one deletes the object then this class member pointer object will become dangling, or if the object is not deleted properly then there will be memory leak. Please suggest the design change required to handle or avoid this situation

1654


What is encapsulation oop?

569


How do you explain polymorphism?

580


What is object and example?

596


What is difference between data abstraction and encapsulation?

610


What is the benefit of oop?

566


What is polymorphism what are the different types of polymorphism?

553