English 中文(简体)
Can I make a protected member public in Java? I want to access it from a subclass
原标题:

I m new to Java and OOP,

I was using a private subclass (actually a struct) B in a class A, and everything went well until I decided to make a parent class C for subclass B. I want make public some of the protected members of class C.

For example:

public class A {
   private class B extends C {
       public int product;
       public int x;
       public int y;
       public void add() {
             product=x+y;
       }
   }
   B b=new B;
   b.x=1;
   b.y=2;
   b.multiply();
   System.out.println(b.product+"="+b.x+"x"+b.y);

public class C {
   protected int x;
   protected int y;
   public int sum;
   public C(px,py) {
       x=px;
       y=py;
   }
   public void sum() {
       sum=x+y;
   }
}

And I get

Implicit super constructor C() is undefined for default constructor. Must define an explicit constructor

Of course, I could remove extends C, and go back to what I had before. Or I could make a getter/setter. But I think it is understandable that an inner struct is acceptable, and it should be able to extend other classes.

最佳回答

The compiler message is reasonably clear - in B you ve effectively got:

public B() {
    super();
}

and that fails because there s no parameterless constructor in C to call. Either introduce a parameterless constructor, or provide an explicit constructor in B which calls the constructor in C with appropriate arguments.

I m not sure it s a good idea to have all these non-private fields, mind you - nor is it a good idea for fields in B to hide fields in C. Do you really want an instance of B to have two x fields and two y fields? You realise they will be separate fields, don t you?

If you just want to effectively provide public access, you could have:

public void setX(int x) {
    this.x = x;
}

public int getX() {
    return x;
}

(and the same for y) and remove the extra fields from B. You can t change the actual accessibility of the fields in C though.

问题回答

Okay, I was fuddling with my own code and found that the problem is I needed a protected default constructor for superclass C. It works now...





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签