Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
class Base
{
    public void Method()
    {
        // ...
    }
}

class Derived : Base
{
    public void Method()
    {
        // ...
    }
}

main()
{

}


so in main function How Can i access base class method having same name in derived class using derived class object.
Posted
Updated 9-May-17 13:27pm
v3

base.method();

Seems I need to add more content, but there's nothing more to say.
 
Share this answer
 
Comments
Sauro Viti 6-Aug-10 4:40am    
I think that the enquirer doesn't want to call base.Method() from "inside" the derived class...
jayant812 6-Aug-10 4:47am    
ya that i know we can accesss like "base.method();" in derived class
but i want to create object of derived class in main function and using that object i want to access base class method how?
koool.kabeer 6-Aug-10 5:06am    
use other function in derived class to call the base.Method();
class base
{
public void method()
{
....
....
}
}

class derived:base
{
public override void method()
{
....
....
}
public void Method2()
{
base.Method();
}
}

main()
{
derived d = new derived();
d.Method2();
}
The trick with Delphi used to be to simply cast it back to it's base class and call from there. This worked exceptionally well for properties that where hidden by certain components.

You could give it a try :)

(base)derivedObject.method();


good luck!
 
Share this answer
 
v2
You cannot! If a class inherit from a base class and override some methods of the base class, it's assumed that the new class extends the base class: the base class method knows how to deal with the base class, but that could not be a valid operation to be done on the inherited class!

What you can do is this:

C#
class Base
{
   public virtual void Method()
   {
      // Do something specific to Base class
      ...
   }
}

class Derived : Base
{
   public override void Method()
   {
      // This will call Base.Method() ...
      base.Method();

      // ... and now we extend the functionalities of Method() by doing something more specific to Derived class
      ...
   }
}
 
Share this answer
 
v2
Comments
Toli Cuturicu 6-Aug-10 8:10am    
Reason for my vote of 1
false
C#
Derived derived1 = new Derived();
(derived1 as Base).Method();
 
Share this answer
 
Comments
Sauro Viti 6-Aug-10 8:32am    
Reason for my vote of 1
Your solution works as long as the method has not been declared as virtual.
Anyway doing that is a bad programming technique and it breaks the principles of OOP.
The C# compiler allow you in doing it, but it give you a warning (CS0108) if you redefine a method that is not virtual without using the new keyword.
Sauro Viti 6-Aug-10 8:45am    
Wow! What a great idea! Encapsulation and polymorphism are two basics on OOP! If an enquirer ask about how to break them a wise developer should suggest him to avoid it and why, not simply tell him how to get in trouble!
Toli Cuturicu 6-Aug-10 9:41am    
> Your solution works as long as the method has not been declared as virtual.
Well... the method was NOT declared virtual, so...
> Anyway doing that is a bad programming technique and it breaks the principles of OOP.
I know. I don't do this. jaiant812 want to do it.
And about the warning:
1. A warning is not an error.
2. It can be safely ignored.
3. It can be suppresed by using pragma warning directive.
Toli Cuturicu 6-Aug-10 9:44am    
But your answer was really wrong. "You cannot!".
Well, you can (although it is not advisable).
That's why I voted 1.
But you voted 1 out of pure revenge.
Sauro Viti 6-Aug-10 10:07am    
Ok, I'm wrong because technically speaking it's possible, and you know that doing it is a very bad approach.
You say "I know. I don't do this. jaiant812 want to do it.", then if someone say "I want to commit suicide" a good answer is "buy a gun"? Obviously this is an exaggerate example, but this is the reason of my vote 1: to be a good answer you should specify "you can do it this way, but it's not advisable because...", so that the enquirer could really learn about OOP.
Then we both deserve a bad vote! ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900