Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
see the code below
public class ClassA
   {
       public void MethodA()
       {
           Console.WriteLine("Class A Method Called");
       }

   }

   public class ClassB : ClassA
   {
       public void MethodB()
       {
           Console.WriteLine("Class B Method Called");
       }

   }

Now if i've created object of ClassB as

ClassB objClassB = new ClassB();


and i want to access the method of classA through this objClassB object

(want to access MethodA() with objClassB )

How i can do it without creating instance of ClassA
Posted
Comments
dimpledevani 2-Aug-12 1:50am    
You can directly call the method the way you call it from Class A. e.g : objClassB.MethodA();

Since you are using inheritance here, you don't need to create the object of ClassA. In the case of inheritance all the methods of Parent Class (in your case ClassA) will be accessible in child class(in your case ClassB). Hence, you can directly call the function of ClassA in ClassB, Like:
C#
public class ClassB : ClassA
{
    public void MethodB()
    {
        Console.WriteLine("Class B Method Called");
        MethodA();
    }
}




--Amit
 
Share this answer
 
The method in question is not a static method. Such methods are called "instance method". That means, they need an instance for a call. Actually, this instance (something on the left side of the dot ('.'), like in the call someInstance.SomeMethod()) is passed during the call to the instance method as an additional (hidden) parameter. In the method implementation, this instance is known by a keyword "this" (which is usually omitted where it is unambiguous).

Look at your method ClassA.MethodA: you never use "this" (that is, never use any instance members of ClassA. So, having an instance method here makes no sense; you can declare MethodA as static. This way, you will perform the call without an instance: ClassA.Method(A).

Conclusions:

1) You cannot do it with MethodA as it is an instance method.

2) You should declare this method as static, and then you can.

—SA
P. S.: If you call it OOP, you are gravely mistaken. :-) You are not even close yet. "Real" OOP will start when you start overriding abstract/virtual method, and not formally, but where it is really critical to have late binding. Right now, you are pretty far from this step, it looks like. Not to worry: you should move there, eventually.
 
Share this answer
 
v2
ClassB IS a classA. So you can just call MethodA, it's there. That's what a derived class is. Your ClassB IS a ClassA, by definition.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Aug-12 2:04am    
True, but formally, the call still needs instance. And this instance will be of type ClassB and of ClassA as well, by the reason you explain. So, what you explain is not quite enough. Please see my answer.
--SA
Christian Graus 2-Aug-12 2:24am    
He was specific enough that he wants to call methoda with an instance of class B, so your answer was correct, but probably gave him more info than he needs, and probably confused him :-)
Sergey Alexandrovich Kryukov 2-Aug-12 2:39am    
Thank you for the note. Confused or not -- my role is to answer. Those who did not get it can ask some follow-up questions...
--SA

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