Biju Joseph N
Biju Joseph N

Reputation:

Force to call a parent class method when the child class method is invoked - C#.NE

We have two classes say 'Parent' and 'child' as shown below :

class Parent
{
   parent_AddUser()
    {
      // add user code for parent
    }

    abstract child_AddUser();
}

// child class 
class Child : Parent
{
  child_AddUser()      // implementing the abstract method
   { 
     // child addUser code  
   }
}

We would like to know whether we can force calling the parent_AddUser() method whenever the child_addUser() is invoked wihout making an explicit call.

Upvotes: 6

Views: 1480

Answers (7)

IordanTanev
IordanTanev

Reputation: 6240

yes it is possible like this with work arround like this

class Parent { parent_AddUser() { // add user code for parent

}

protected virtual child_AddUser(){
//does smethins
}
}


class Child: Parent
{
protected override child_AddUser()
{
// does something
base.child_AddUser();
}
}

Upvotes: 0

JDunkerley
JDunkerley

Reputation: 12505

You could do something like:

class Parent
{   
    parent_AddUser()
    {      // add user code for parent    }    

    public child_AddUser()
    {
        this.parent_AddUser();
        this.doChildAddUser();
    }

    protected abstract doChildAddUser();
}

// child class 
class Child : Parent
{  
    doChildAddUser()      // implementing the abstract method   
    {      // child addUser code     }
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062865

If you want to enforce logic in the child class, then something like:

class Parent
{
    public void AddUser()
    {
      // pre-logic here
      AddUserCore();
      // post-logic here
    }

    protected abstract void AddUserCore();
}

// child class 
class Child : Parent
{
   protected override void AddUserCore()
   { 
     // child addUser code  
   }
}

Then no need to call the parent methods; already done in "pre-logic" and "post-logic"

Upvotes: 0

Timbo
Timbo

Reputation: 28050

You have to make child_Adduser non-public and add a third method that controls the whole add-user action:

class Parent
{
    private void parent_Adduser()
    {
        // ...
    }

    protected abstract void child_Adduser();

    public void Adduser()
    {
        parent_Adduser();
        child_Adduser();
    }
}

class Child
{
    protected override void child_Adduser()
    {
        // ...
    }
}

This way, when Adduser() is called always both the parent and child part is executed.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500695

The way to do this is to use the template method pattern instead:

public abstract class Parent
{
    public void AddUser()
    {
        // Preparation goes here
        AddUserImpl();
        // Clean-up goes here
    }

    protected abstract void AddUserImpl();
}

public class Child
{
    protected override void AddUserImpl()
    {
        // Do stuff here
    }
}

That way no-one other than Parent calls AddUserImpl - callers just use AddUser which delegates to the concrete subclass for just the right bit.

Upvotes: 9

h0b0
h0b0

Reputation: 1869

As far as I know, this is not possible. But what you can do is to use the Template Method pattern.

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

No, there is no way to do this. You will need to add a call to parent_AddUser in child_AddUser.


You can do something similar, using the Template Method Pattern.

public abstract class Parent {
    public void AddUser(User user) {
        // Do parent stuff
        AddUserImpl(user);
        // More parent stuff
    }

    protected abstract void AddUserImpl(User user);
}

public class Child {
    protected override void AddUserImpl(User user)
    {
        // Do child stuff
    }
}

Upvotes: 1

Related Questions