I was wondering if there’s a language feature in Java in which methods of a superclass would be invisible for members of a subclass:
public class Subclass extends protected Superclass
or something. I’ll give an example.
Here is your superclass.
public class A{ public String getA(){...} public String getB(){...} public String getC(){...} public void setA(String a){...} public void setB(String b){...} public void setC(String c){...} }
If you want to subclass A while protecting some of its methods, and you can’t change access modifyers in methods unless you override them, you’d end up with something like this-
public class B extends A{ private String getA(){return super.getA();} private String getB(){return super.getB();}//These four methods have private void setA(String a){super.setA(a);}//to be redeclared. private void setB(String b){super.setB(b);} public String getC(){return super.getC();}//These two methods can be public void setC(String c){super.setC(c);}//removed. public String getD(){...} public void setD(String d){...} }
Either that or you can keep a private instance of A and have something like this:
public class B{ private A obj; private String getA(){return obj.getA();} private String getB(){return obj.getB();}//These four methods can also private void setA(String a){obj.setA(a);}//be removed. private void setB(String b){obj.setB(b);} public String getC(){return obj.getC();}//These two methods are public void setC(String c){obj.setC(c);}//redeclared. public String getD(){...} public void setD(String d){...} }
Can you have something that takes both in a way that you don’t have to redeclare any methods?
There is no ‘non-public’ inheritance in Java, unlike the situation in C++.
Inheritance creates a subtyping relation. Any instance of B is also an instance of A, and should respond to the same messages. If instances of B clearly don’t respond to all messages that instances of A respond to, then inheritance would be inappropriate anyway.
Your last solution (B does not inherit from A) is the appropriate one: you don’t create a subtyping relation, just use one type to (secretly) implement the other.