SmRndGuy
SmRndGuy

Reputation: 1819

Java: extending an object to another

For example I have this:

class A{
    private int mine = 0; //Some field...
    public A(int a){mine+=a; /*Some operation1...*/}
}


class B extends A{
    private int mine = 0; //Some field...
    public B(int a){mine-=a; /*Some operation2...*/}
}

and I get:

error: constructor A in class A cannot be applied to given types;
    public B(int a){}
    required: int
    found: no arguments
    reason: actual and formal argument lists differ in length
    1 errors

I don't understand the error? What is telling me to do?

Though, the code works if Constructor of "A" has no arguments.
But I need to do the Operation1 (aka mine+=a;), so I need A's arguments, but then I fail.

I'm closed in this magic circle. What shall I do?

Upvotes: 2

Views: 121

Answers (4)

user unknown
user unknown

Reputation: 36229

If B extends A, the first operation of the constructor of B is, to call the constructor of A.

If there is no constructor explicitly defined in A, the standard constructor is called. If you don't call the ctor explicitly, it is called implicitly.

But if there is an explicit ctor, which takes arguments, it isn't called automatically - how should it - with which arguments? You have to call it yourself.

But your class hierarchie is somewhat misterious. If you want to access mine in the subclass, make it protected or leave it in default state - don't make it private.

If you make it private, a new introduced attribute with the same name hides the parent attribute, but this is errorprone and irritating.

In most cases, access, maybe via a method (getter/setter), would be better, or opening the visibility.

Or a different name for the thing in the subclass.

Upvotes: 1

Martin
Martin

Reputation: 1140

I think you need to call class A's constructor from B constructor, like this: super(a);

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691625

The first instruction of every constructor is always to invoke one of its superclass constructor. If you don't do it explicitely, the compiler inserts this instraction for you. The constructor

 public B(int a) {
     mine-=a; 
     /*Some operation2...*/
 }

is thus equivalent to

public B(int a) {
    super(); // invoke the no-arg super constructor
    mine-=a; 
    /*Some operation2...*/
}

Since A doesn't have a no-arg constructor, the compilation fails. In this case, you must explicitely invoke one of the super constructors:

public B(int a) {
    super(a);
    mine-=a; 
    /*Some operation2...*/
}

Upvotes: 9

Michael Borgwardt
Michael Borgwardt

Reputation: 346240

Every constructor must call a superclass constructor as the first thing it does. If you do not do it explicitly via super(), the compiler will implicitly call the no-arts constructor in the superclass. But in your case, the superclass does not have a no-args constructor, at which point the compiler gives up.

By the way, it's generally a very bad idea to re-declare a field in a subclass, because it will hide the superclass field, but there will still be two fields.

Upvotes: 7

Related Questions