constructors not inherited ?

C

chris sennitt

the following code fails unless i create the ( commented out ) constructor
in bClass
does Java really not have the ability to inherit constructors ?

a = new aClass("foo");
b = new bClass("foo");
....

class aClass {
public aClass( String sName ) {
System.out.printline("constructor ="+sName );
}
}
class bClass extends aClass {
// public bClass( String sName ) {
// super( sName );
// }
}
 
J

Joona I Palaste

chris sennitt said:
the following code fails unless i create the ( commented out ) constructor
in bClass
does Java really not have the ability to inherit constructors ?

I think you are right, constructors are not implicitly inherited. But
why should they be? They're only ever called when creating a new object,
and then you have to know the correct class anyway.
 
C

Christophe Vanfleteren

chris said:
the following code fails unless i create the ( commented out ) constructor
in bClass
does Java really not have the ability to inherit constructors ?

a = new aClass("foo");
b = new bClass("foo");
...

class aClass {
public aClass( String sName ) {
System.out.printline("constructor ="+sName );
}
}
class bClass extends aClass {
// public bClass( String sName ) {
// super( sName );
// }
}

As you can see, no, constructors are not inherited.
 
C

chris sennitt

"Joona I Palaste" said
I think you are right, constructors are not implicitly inherited. But
why should they be? They're only ever called when creating a new object,
and then you have to know the correct class anyway.

i can think of many examples in other oop langauges i have used

class A has some instance variables methods and a constructor
class B extends A and overrides a DRAW method - thats the only difference
between these two objects that i need so the original constructor is perfect
as is.

however, it appears i also have to implement the SAME constructor method,
either block copy it or create a dummy constructor and call the super class
constructor.

it seems i am having to do the work the language should do for me.
 
T

Tony Morris

chris sennitt said:
the following code fails unless i create the ( commented out ) constructor
in bClass
does Java really not have the ability to inherit constructors ?

a = new aClass("foo");
b = new bClass("foo");
...

class aClass {
public aClass( String sName ) {
System.out.printline("constructor ="+sName );
}
}
class bClass extends aClass {
// public bClass( String sName ) {
// super( sName );
// }
}

If you have a class that does not have a constructor provided (as you do
when you leave your comments in), a public default constructor is implicitly
provided that has a null implementation.

Therefore,
class bClass extends aClass
{

}

is equivalent to:
class bClass extends aClass
{
public bClass()
{

}
}

Also, if a constructor does not have an implicit call to a superclass
constructor or 'this' constructor, an implicit call to super() is supplied.

Therefore,
class bClass extends aClass
{

}

is also equivalent to:
class bClass extends aClass
{
public bClass()
{
super();
}
}

Now, when you look at that call to super(), you might want to speculate to
yourself why this might cause a compile-time error (before I tell you the
answer).

Your superclass (aClass) does not provide a default constructor (a
constructor without arguments), therefore, the implicit call to super()
fails at compile-time (since super() doesn't exist).


--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
T

Tony Morris

chris sennitt said:
the following code fails unless i create the ( commented out ) constructor
in bClass
does Java really not have the ability to inherit constructors ?

a = new aClass("foo");
b = new bClass("foo");
...

class aClass {
public aClass( String sName ) {
System.out.printline("constructor ="+sName );
}
}
class bClass extends aClass {
// public bClass( String sName ) {
// super( sName );
// }
}

One more thing, no constructors are not inherited (but this is not the
reason for your compile error).

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
J

Joona I Palaste

chris sennitt said:
"Joona I Palaste" said
i can think of many examples in other oop langauges i have used
class A has some instance variables methods and a constructor
class B extends A and overrides a DRAW method - thats the only difference
between these two objects that i need so the original constructor is perfect
as is.
however, it appears i also have to implement the SAME constructor method,
either block copy it or create a dummy constructor and call the super class
constructor.
it seems i am having to do the work the language should do for me.

What's so horribly bad about a constructor that does nothing except call
the super constructor? How long time would it take to write three lines
of code?
 
J

Joe

the following code fails unless i create the ( commented out ) constructor
in bClass
does Java really not have the ability to inherit constructors ?

You are implementing wrong. Since bClass extends aClass, the constructor
with a single String param is already available to both classes as
defined by aClass. You are trying to redefine the constructor for that
particular parameter set so, by definition bClass does NOT extend
aClass. Extending means that bClass IS an instance of aClass. Rewriting
that constructor changes that relationship.
 
T

Tor Iver Wilhelmsen

Joe said:
You are implementing wrong. Since bClass extends aClass, the constructor
with a single String param is already available to both classes as
defined by aClass.

No, constructors are *not* inherited. Nor are they considered virtual
methods. If you do not provide a constructor in class Foo, the
compiler will synthesize

public Foo() {
super();
}

which will fail in compilation if the superclass doesn't have a
parameterless constructor.

A superclass' constructors are only available to a class through the
super keyword.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top