Paul-Peter Tournaris wrote:
No you don't need do. But if you want to call the base constructors you
just call super with the appropriate parameters.Also you can create new
constructors for your new class that extends the base one! For example!
This is basic Java. Check out http://docs.oracle.com/javase/tutorial/
public class base {
Please conform more or less to the Java coding conventions, particularly
with regard to naming:
http://www.oracle.com/technetwork/java/codeconv-138413.html
You don't extend constructors in Java.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8
Constructor declarations are not members. They are never inherited and
therefore are not subject to hiding or overriding.
So the original question is flawed.
What's going on is that
public base(){
int i = 0;
}
}
public class newBase extends base {
public newBase(){
super();//if you want to call the mother constructor
This is done for you automatically by the compiler. Adding 'super()' to a
constructor
implementation is a rookie move.
You need to add a super-constructor call when you need to specify an
overload of
the parent constructor, i.e., one with parameters. For the no-arg
constructor it's a waste
of boilerplate.
int i=0;
How does this help the pedagogy?
}
}
Simon Giddings wrote:
This may seem a bit basic, but I come from a C++ background and it is
confusing me a little.
When I extend a base class, do I need to create a constructor for each
base class constructor ?
You don't.
You only need to create a constructor that is required by the child class
itself. It is not uncommon
to use a no-arg child-class constructor that invokes a superclass
constructor with arguments.
public class DynamicTypeT
{
final Class? extends T token;
protected DynamicType(Class? extends T tok)
{
if (tok == null) { throw new IllegalArgumentException(Null token); }
token = tok;
assert token != null : No IllegalArgumentException;
}
// more stuff ...
}
public class DynamicFoo extends DynamicTypeFoo
{
public DynamicFoo()
{
super(Foo.class);
}
// more stuff ...
}
What I mean is this. Given :
public class base
{
protected int m_iValue;
Please follow the naming conventions.
public base()
{
m_iValue = 0;
}
public base(int iVal)
{
m_iValue = iVal;
}
}
If I want to create a new class based on this base class, will I need to
declare the two constructors again for them to be visible ?
They won't be visible even if you do declare a bunch of constructors in the
child class.
Constructors are not inherited.
The following seems to hide the second constructor of the base class.
Doesn't.
public class Derived extends base
{
public Derived()
{
super();
}
}
You only declared one constructor. You didn't hide any. If you want a
constructor
that takes a parameter, you have to declare it. Check out the Java
tutorials.
What is the correct way to do this ?
Forget C++.
Since constructors aren't inherited, you must explicitly call any no-arg
super
constructor that you wish used. You must write every constructor needed by
the class itself. You don't need to invoke any super-constructors the child
class doesn't need.
BIG WARNING!
Do not do too much in a constructor. Constructors' roles should be
constrained to setting up INITIAL conditions only, and only those needed
to construct the object itself, at that. It is a common mistake to cram too
much logic into the constructor. The danger is that such logic perforce
operates on an incomplete instance of 'this'. That is very bad.
CONSTRUCT IN CONSTRUCTORS. PERFORM IN METHODS.
--
Lew
--
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en