开发者

Java Constructor error

I am getting a constructor error. To make this code work, do I need to use super in class B?

 class A {  
      protected String s ;
      A (String s) {
           this.s = s  ; 
         }
  }

 class B ext开发者_开发问答ends A {
     String s ;
     B(String s) {
        this.s  = s ;    
         }
   }


Yes, you need to call the super-constructor from B's constructor. If you don't specify either this(...) or super(...) at the start of a constructor, an implicit call to super() will be inserted for you - a call to the parameterless constructor of the superclass. In this case you don't have a parameterless constructor in A - so you need to explicitly specify the constructor you want to call, along with the arguments.

Chances are you don't want another variable called s within B, though...

I suspect you want this:

class B extends A {
    B(String s) {
        super(s);
    }
}

It's important to understand what would happen if you did also declare a variable called s in B. You'd then have two independent variables for each instance of B - the one declared in A and the one shadowing it in B. They could easily take different values... which would be extremely confusing.

Note that additionally it's almost always a good idea to make fields private - at which point you don't really know which variables your superclass declares, as you can't access them. If you happen to shadow a variable, that at least doesn't lead to any apparent ambiguity (which is, of course, handled by the specification). It's still typically a mistake for one variable to have the same name as a variable in its superclass though - it suggests you've got two different sources of truth for the same information.


Yes, you need to make a call to Class A using super();

Remember, your subclass will provide an automatic ( super() ) call to superclass constructor, only and only if you have a no-arg constructor in superclass.

Once you have defined a parameterized constructor for your superclass, your subclass needs a super(); call from its constructor. Whenever your subclass inherits from a Super class, you need to make a call to Superclass using a super();.

You need to re-write your Class B code like this :

class B extends A {
    B(String s) {
        super(s);   // Calls A's constructor ..
    }
}

Note : super(s); has to be the first statement in the B's constructor.


Short answer: yes.

All subclasses need to include a call to a superclass' constructor as their first action. If you don't insert a call explicitly, then a call to super() is inferred.

In the case where the superclass doesn't have a no-arg constructor, you'll need to supply the arguments (and thus make the call) yourself, like so:

class B extends A {
    String s;
    public B(String s) {
        super(s); // Or pass in any other argument to super's constructor
        this.s = s;
    }


class A {
    protected String s ;
    A(String s) {
        this.s = s;
    }
}
class B extends A {
    B(String s) {
        super(s);
    }
}

Your field String s in class B hides the field by the same name in class A. If you need a separate String field in B, you should name it something else.


First of all, you don't need to make the same variable and constructor in B, because they already exist (B extends A). You can do something like this:

public class test {
A a = new A ("This is a");
B b = new B ("This is b");
    private class A {  
        protected String s ;
        A ( String s ) {  this.s = s; }
        public String getS() { return s; }
    }
    private class B extends A {
        B (String s) { super (s); }
    }

    public static void main (String [] args) {
        test t = new test();

        System.out.println(t.a.getS());
        System.out.println(t.b.getS());

        /*
Output will be This is a and This is b
         */
    }
}

You are right that you have to call the super(s);

Good luck!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜