开发者

Java - Extends issue

开发者 https://www.devze.com 2023-01-28 07:31 出处:网络
I am having a hell of a time with an "extends" issue with a problem in a problem set I\'m working on - I think I\'m just having a block because it\'s written to be purposefully confusing. He

I am having a hell of a time with an "extends" issue with a problem in a problem set I'm working on - I think I'm just having a block because it's written to be purposefully confusing. Here's the problem as I was given it:

class A {
    int x;
    A(int a) {System.out.println(" class A");}
}

class B extends A {
   int x;
   B() {System.out.println(" class B");}
   public static void main (String [] args) {
    A a = new B();
    }
}

When I compile, I get the following error kicked out from the con开发者_JS百科sole:

cannot find symbol
symbol  : constructor A()
location: class A
   B() {System.out.println(" class B");}
       ^

and I'm supposed to be able to fix this error in class B without touching class A. I'm clearly missing something stupidly obvious, but I've tried permutations of everything I can think of for an hour and nothing's working.

So far, I've tried:

  • throwing a void in front of B() - same error.
  • making B() into A(int a) - the latter comes back with an invalid method declaration error (adding a void in front of it kicks me back to the original "cannot find symbol" error)
  • making B() B(int a) - same error, plus throws an additional "cannot find symbol" error since I've now trashed the B() declaration.
  • making class B extends A into class B extends A(int A) - throws nine errors. Clearly not the answer.
  • Changing the class name of A to Apple just to see if that would give me an answer - this wants me to add a void in front of A(int a) in the Apple class, which I can't do.

What the hell am I missing here? This is not exactly an advanced class so it can't be anything terribly complicated, but this is making me completely insane. Any help would be greatly appreciated.


In a derived class, you need to call the base class constructor. If you don't do this explicitly, the compiler will try and insert a call to the no-argument constructor - if none exists in the base class, you get that error.

The solution is to explicitly call the base class constructor with some value:

B() {
    super(0);
    System.out.println(" class B");
}


your first call in B's constructor needs to be super(a); a being whatever you want to input into B's constructor.


1) When you extend a class, your subclass implicitly contains a chunk of data that consists of all the data used to make an instance of the superclass. You have to initialize that data, too. If you don't say how it will be initialized, it is assumed you want the default constructor, i.e. A.A(). But this constructor does not exist - you only have A.A(int).

To do this initialization, as the first line in the B constructor, you make a call to the A constructor, with special syntax as follows:

B() {
    super(42); 
    // because we are calling the constructor that takes an int, we must supply
    // one. It's up to you to figure out what values should be supplied. Maybe 
    // you wanted to take an int in the B constructor, and pass it along?
    System.out.println(" class B");
}

2) You have an int x; declared in both class A and class B. This is probably not what you want. As noted, each B instance already contains an int x - the one that it automatically gets because B extends A.


Class A does not have a default constructor because you didn't write one (but you did write one that takes an int parameter).

Class B has a no-arg default constructor. It needs to call a superclass constructor, but it can't because there is no such thing in class A.

0

精彩评论

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

关注公众号