1

I have this code

class parent 
{
  public void sleep()
   {
     // Some Logic
   }
}

class Child : Parent
{
  public void sleep()
  {
    // some logic
  }
}

class Implement
{
  Child ch = new Child();
  ch.sleep();
}

But now I want to access sleep() method of parent class by using instance of child class which is created already.

Damien
  • 8,889
  • 3
  • 32
  • 40
vaibhav shah
  • 4,939
  • 19
  • 58
  • 96
  • 1
    This arrangement results in `Child` [shadowing](http://stackoverflow.com/questions/392721/difference-between-shadowing-and-overriding-in-c) the base class's `sleep` method, which is an unusual arrangement. The compiler will warn you by saying that you should add `new` to the child method declaration if that's what you *really* want. Is it? – Jon Aug 14 '13 at 13:06
  • 2
    Do you want the ability to call either method from a `Child` object (if so why did you choose the same name?) Or do you just want the child class to always call the parent's sleep method with added logic? – Kevin DiTraglia Aug 14 '13 at 13:06
  • Access how? do you expect ch to somehow access the method of `Parent` or you just want to access parent method **inside** `Child` instance. – Claudio Redi Aug 14 '13 at 13:08
  • Like Kevin mentioned, if they are doing different things why don't you name the second method something else? – reggaemahn Aug 14 '13 at 13:10

6 Answers6

9

You can cast your object to parent type.

Child ch = new Child();
var parent = (Parent)ch;
parent.sleep();
3

You just need to cast the created Child object to Parent:

((Parent)ch).sleep();

As @Thorsten commented below, this works because Parent.sleep is a non-virtual method and it is not overridden in the Child class. If it were overriden, then there would be no way for you to call the Parent.sleep implementation using ch. (For virtual methods, the method that is invoked is the "most derived implementation", that is the most derived implementation of this method within the class hierarchy with the override keyword.)

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
1

To access parent member you must cast the object.

public class A
{
   public virtual void One();
   public void Two();
}

public class B : A
{
   public override void One();
   public new void Two();
}

B b = new B();
A a = b as A;

a.One(); // Calls implementation in B
a.Two(); // Calls implementation in A
b.One(); // Calls implementation in B
b.Two(); // Calls implementation in B

Source: new keyword in method signature

Suggestion: you're hidding an inherited member. You should use "new" keyword like this:

    class Parent
    {
        public void MethodA()
        {
            Console.WriteLine("Parent");
        }
    }

    class Child : Parent
    {
        public new void MethodA()
        {
            Console.WriteLine("Child");
        }
    }
Community
  • 1
  • 1
Vismari
  • 745
  • 3
  • 12
0

You could create a new method on Child called ParentSleep like so:

class Child : Parent
{
  public void sleep()
  {
    // some logic
  }

  public void ParentSleep()
  {
     base.sleep();
  }
}

Then call it like so:

Child ch = new Child();
ch.ParentSleep();
lehn0058
  • 19,977
  • 15
  • 69
  • 109
  • You could, but that sucks. – Lews Therin Aug 14 '13 at 13:12
  • This is technically correct but wrong in just about any other way. If the child class does not want to hide the parent's `sleep` method it should not shadow it. And if it does want to hide then it does not make any sense to expose it like this. – Jon Aug 14 '13 at 13:12
  • We do not know why the method is being shadowed, if it is intended to hide the parent method, or the conditions as to what the underlying reason for wanting to do the above. I was just providing a possible solution of doing it. – lehn0058 Aug 14 '13 at 13:15
  • @lehn0058: If someone asked how to sort an array and you answered with [bubble sort](http://en.wikipedia.org/wiki/Bubble_sort) you should expect people to comment that while that would work, noone in their right mind should choose to do it unless for educational purposes. The same goes here. – Jon Aug 14 '13 at 13:22
0

if you want to implement this with interface as well as Parent Class you can do something like this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExplicitInterfaceImplementation
{
    class Program
    {
        static void Main(string[] args)
        {
            SampleClass sc = new SampleClass();
            var mc = (MainSampleClass)sc;
            IControl ctrl = (IControl)sc;
            ISurface srfc = (ISurface)sc;

            mc.Paint();
            sc.Paint();
            ctrl.Paint();
            srfc.Paint();
            Console.ReadKey();
        }
    }

    /// <summary>
    /// Interface 1
    /// </summary>
    interface IControl
    {
        void Paint();
    }

    /// <summary>
    /// Interface 2
    /// </summary>
    interface ISurface
    {
        void Paint();
    }

    /// <summary>
    /// Parent/Main Class 
    /// </summary>
    public class MainSampleClass
    {
        /// <summary>
        /// Parent class Paint Method.
        /// </summary>
        public void Paint()
        {
            Console.WriteLine("Paint method in - Parent MainSampleClass");
        }
    }

    /// <summary>
    /// SampleClass is the Child class inherited from Parent/Main Class and two interfaces
    /// Parent/Main class having a Paint() method and two interfaces having 
    /// Paint() method - each of them having same name but they are not same(different from each other).
    /// </summary>
    public class SampleClass : MainSampleClass,IControl, ISurface
    {
        /// <summary>
        /// new method(Paint()) for Child class, separate from parent class(Paint() method)
        /// </summary>
        public new void Paint()
        {
            Console.WriteLine("Paint method in - Child SampleClass");
        }

        /// <summary>
        /// Implementing IControl.Paint() method.
        /// </summary>
        void IControl.Paint()
        {
            System.Console.WriteLine("Paint method in - IControl Interface");
        }

        /// <summary>
        /// Implementing ISurface.Paint() method. 
        /// </summary>
        void ISurface.Paint()
        {
            System.Console.WriteLine("Paint method in - ISurface Interface");
        }
    }
}
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
0
class Implement
{
    Parent parentChild = new Child();
    parentChild.sleep();
}
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
sam
  • 149
  • 4
  • 5
  • 10