vaibhav shah
vaibhav shah

Reputation: 5069

how to access method of parent class with instance of child class in c#

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.

Upvotes: 1

Views: 8368

Answers (6)

sam
sam

Reputation: 149

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

Upvotes: 0

Chandan Kumar
Chandan Kumar

Reputation: 4638

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");
        }
    }
}

Upvotes: 0

Eren Ers&#246;nmez
Eren Ers&#246;nmez

Reputation: 39095

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.)

Upvotes: 3

Vismari
Vismari

Reputation: 745

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");
        }
    }

Upvotes: 1

lehn0058
lehn0058

Reputation: 20257

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();

Upvotes: 0

Oleksandr Kobylianskyi
Oleksandr Kobylianskyi

Reputation: 3380

You can cast your object to parent type.

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

Upvotes: 9

Related Questions