Force the method of the parent class when calling the method of the child class - C # .NE

We have two classes: “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 if we can forcefully call the parent_AddUser () method whenever a call to child_addUser () is called without an explicit call.

+6
c #
source share
7 answers

The way to do this is to use the template template template :

 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 } } 

Thus, no one except Parent calls AddUserImpl - callers, simply uses AddUser , which delegates a particular subclass for the correct bit only.

+9
source share

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


You can do something similar using the template template template.

 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 } } 
+1
source share

As far as I know, this is not possible. But you can use the template method template .

+1
source share

yes, perhaps, for example, with such work

 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(); } } 
0
source share

You must make child_Adduser non-public and add a third method that controls the entire user’s actions:

 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() { // ... } } 

Thus, when calling Adduser() , both the parent and child parts are always executed.

0
source share

If you want to use logic in a 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 there is no need to call parent methods; already done in "pre-logic" and "post-logic"

0
source share

You can 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 } } 
0
source share

All Articles