开发者

Is it possible that Different child classes have different visibility to the methods of the Parent in java

开发者 https://www.devze.com 2023-02-02 07:17 出处:网络
Is it possible that different child classes have different visibilities to the methods of the parent. Suppose there is a class A which has 10 methods defined. It hastwo different childClassB and Class

Is it possible that different child classes have different visibilities to the methods of the parent. Suppose there is a class A which has 10 methods defined. It has two different child ClassB and ClassC. Is it possible that that ClassB and ClassC has access to different methods of ClassA. Like ClassB has access to only 6 of 10 methods defined in ClassA and ClassC has acess only to the other 4 methods of ClassA? ClassB and ClassC are in s开发者_JAVA百科ame package.

Thanks, Asit


I don't think it is possible with classes. To segregate functionality you should use interfaces instead of extending classes.

It is quite likely that your class A is violating the Single Responsibility Principal if you need to divide methods like that.

Then look to use composition instead of inheritance to compose complex classes from the simpler ones. Also take a look at the strategy pattern.

Divide your functionality in interfaces like this -

public interface IFlyable
{
    void FlapWings();

    void Fly();
}

public interface IHuntingAnimal
{
    void Hunt();
}

Then implement your classes like this -

public class Duck : IFlyable { ...

public class Eagle : IFlyable, IHuntingAnimal { ...

public class Tiger : IHuntingAnimal { ..

Note: The example is in C#. You need to work out the java equivalent.


You can do such a thing with interfaces, but not concrete classes.


Adapter or Decorator pattern will help you.


From you wrote, i suppose what you have is

package first;
class A {
    protected methodA() {...}
    private methodB() {...}
    public methodC() {...}
    methodD() {...}
}

package second;
class B extends A {...}
class C extends A {...}

In this case, B and C will only see methods methodA and methodC from class A.

methodB is private, so unreachable

methodD is package protected, that's to say restricted to classes in same packgae (a weird way to have an equivalent of the C++ friend keyword, I realize now), and as a consequence not visible outside of packgae first.


not really understood what do you mean...

it is generally possible that methods of a parent class have different access modifiers as possible inherent classes. but with one condition (citation from lang spec):

The access modifier of an overriding or hiding method must provide at least as much access as the overridden or hidden method, or a compile-time error occurs.

you cannot coarct usability of a class, i.e. if a method from your class A was "protected" then you can declare an overriding method in your class B as "public". but it doesn't work vice-versa


I don't know the context of your question so I can't comment as to whether your design is sound and by extension, whether your motivation for this is justified. Since others have already taken the stance that your ClassA requires refactoring, I'm going to do the opposite and assume that it's a sensible class with a single well-defined purpose and it doesn't require subdivision.

In which case, why not use the object adapter pattern to achieve what you're after? You can expose the ClassA methods you want to in your ClassB and ClassC adapters by implementing wrapper methods which forward invocations to your ClassA adaptee. And of course, you can optionally extend those methods.


This answer is predicated on your use of the term child class
Short answer no
Longer answer
In this situation:
class Base has the following methods: method1, method2, method3
There is no way, using Java, to setup the following situation:
class Derived1 (this class extends class Base) can access method1 and method2, but cannot access method3.
class Derived2 (this class extends class Base) can access method1, method2, and method3.
Both classes are in the same package.

In Java, when one class extends another class these things always apply:

  • The derived class may call every public method of the base class.
  • The derived class may call every protected method of the base class.
  • The derived class may not call any private method of the base class.
  • If the derived class is in the same package as the base class, the derived class may call every package access method of the base class.
0

精彩评论

暂无评论...
验证码 换一张
取 消