Mike
Mike

Reputation: 1

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 extends A {
     String s ;
     B(String s) {
        this.s  = s ;    
         }
   }

Upvotes: 0

Views: 421

Answers (5)

Hidde
Hidde

Reputation: 11931

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!

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500385

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.

Upvotes: 5

Saurabh Gokhale
Saurabh Gokhale

Reputation: 46395

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.

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234795

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.

Upvotes: 0

Andrzej Doyle
Andrzej Doyle

Reputation: 103787

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;
    }

Upvotes: 1

Related Questions