Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force child class to implement private method

Tags:

c#

.net

oop

I have the following code:

public abstract class NavEntityController<ChildEntity> where ChildEntity : NavObservableEntity
{
    public abstract void Delete(ChildEntity line);
    public abstract void Update(ChildEntity line);
    public abstract void Create(ChildEntity line);

    public void PushChangesToNav(NavObservableCollection<ChildEntity> lines) 
    {
        foreach (var line in lines)
        {
            line.ErrorLastAction = false;
            EntityState previousState = line.CurrentState;

            try
            {
                switch (line.CurrentState)
                {
                    case EntityState.Unchanged:
                        break;
                    case EntityState.NeedsCreate:
                        Create(line);
                        line.CurrentState = EntityState.Unchanged;
                        break;
                    case EntityState.NeedsUpdate:
                        Update(line);
                        line.CurrentState = EntityState.Unchanged;
                        break;
                    case EntityState.NeedsDelete:
                        Delete(line);
                        line.CurrentState = EntityState.Deleted;
                        break;
                }
            }
            catch (Exception e)
            {
                //...
            }        
        }
    }
}

I need a base class to inherit from this class, like such:

public class NavJobController : NavEntityController<NavObservableJob>
{
    public NavJobController( {}

    public override void Delete(NavObservableJob line) {//Implementation here}
    public override void Update(NavObservableJob line) {//Implementation here}
    public override void Create(NavObservableJob line) {//Implementation here}

    //Other functionality
}

However, I do not want someone to be able to do:

NavJobController j = new NavJobController();
j.Create(new NavObservableJob());

but only would like the following method:

j.PushToNav(); //and any other public functionality in the base class to be available

Essentially, I want to force the child class to implement CRUD operations, without exposing them to the public. The ideal syntax I was hoping for is the following:

private abstract void Delete(ChildEntity line);
private abstract void Update(ChildEntity line);
private abstract void Create(ChildEntity line);
like image 654
David Avatar asked Aug 05 '15 09:08

David


People also ask

Can child classes use private methods?

Since method overriding works on dynamic binding, it's not possible to override the private method in Java. private methods are not even visible to the Child class, they are only visible and accessible in the class on which they are declared.

Can a child override a private method?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there.

Can you call private method of parent class?

Private methods are not inherited, and so cannot be called. If you really want to have access to this method, change its access modifier keyword ( protected or public ). +1 you can only call private methods for classes in the same Java file with the same outer class.

Does abstract class force implementation?

While an abstract class cannot be instantiated, it can have implementation details. The designers of C# chose to force the user to either implement the functionality, or specifically state that the functionality inherited from the interface will not be implemented at this level.


1 Answers

This is why protected visibility exists (visible to both base class and derived classes, but not publicly available to use them from an instance).

If you need to publicly access these methods somewhere in your solution, you may need to use internal instead of protected and make internals visible to some assemblies using [InternalsVisibleTo] attribute.

like image 174
Matías Fidemraizer Avatar answered Oct 10 '22 06:10

Matías Fidemraizer