Is it possible to a class has a method that can only be called by it’s objects and hidden for a subclass object? For instance:
class ClassOne {
... // attributes
public void doSomething() { ... }
}
class ClassTwo extends ClassOne {
... // attributes and methods
}
ClassOne c1 = new ClassOne();
c1.doSomething(); // ok to call
ClassTwo c2 = new ClassTwo();
c2.doSomething(); // forbidden
I know this seems weird thinking in therms of inheritance, but is it possible?
PS: the objective of this question is just to learn more about inheritance of OO programming.
It’s not possible and your right that this would break inheritance. Think about
This has to work because ClassTwo is a ClassOne. Edit: at least is has to compile, if the method is overridden and does something else is up to your design. But you cannot make the compiler produce an error on this.