Is there a way to disallow access to a Parent function in the Child class?

For example, the Parent class has a function, myFunc(). In main() after I instantiate a new Child object I don't want myFunc() to even be an option. So child.myFunc() would cause a compilation error.

Recommended Answers

All 5 Replies

declare the method private.

I should have specified, without declaring the method in the Parent class private.
If you were talking about declaring it private in the Child class then that was one of the first things I tried and it still calls the Parent class public function.

Example:
private new void myFunc()
-this causes child.myFunc() to use the Parent functionality

Any other ideas?

Do you want to override the method in the parent class, or not have access to it at all? And no, modifiers in the child class have no effect on the parent class methods.

Do you want to override the method in the parent class, or not have access to it at all? And no, modifiers in the child class have no effect on the parent class methods.

I'm not sure if it's possible but I want the Child class to make the parent method unaccessible to the application programmer.

So
Parent P = new Parent();
Child C = new Child();

P.myFunc() // This is ok
C.myFunc() // I want this to be disallowed, meaning this would cause a compile error

Hopefully this explains it better.

The only way to make it issue a compiler error is to declare the method private in the parent class.

You can override the method in the child class and throw an exception if you want to prevent people from using it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.