Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: OOP
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 1-Aug-12 20:49pm
Comments
dimpledevani at 2-Aug-12 1:50am
   
You can directly call the method the way you call it from Class A. e.g : objClassB.MethodA();
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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. Smile | :) 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.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
public class ClassB : ClassA
{
    public void MethodB()
    {
        Console.WriteLine("Class B Method Called");
        MethodA();
    }
}
 

 
--Amit
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 2 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100