Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

abstract class can not provide constructor for sub class?

Thread view: 
Kevin - 24 Jul 2006 03:44 GMT
abstract class A
{
 public A()
{
}
 public A(int i, int j)
{
}
}

public class B extends A
{

}

B does not have the corrresponding constractor of B(int i, int j) from
A, though B may have all other fucntions of A.

That's to say:
B b = new B(10, 14); will not work.

Isn't it strange?
I found the behavior of constractors are pretty different from normal
functions. How to explain it?

Thanks!
Kevin - 24 Jul 2006 03:47 GMT
PS: does this mean that, I can not use the constructor to provide some
operations for all the subclasses?
Kevin - 24 Jul 2006 04:05 GMT
Well, my purpose is to make sure all the subclasses will have one
uniform constructor (which takes certain parameters). How can I do
that?

Thanks. :-)

> PS: does this mean that, I can not use the constructor to provide some
> operations for all the subclasses?
puzzlecracker - 24 Jul 2006 04:12 GMT
> Well, my purpose is to make sure all the subclasses will have one
> uniform constructor (which takes certain parameters). How can I do
[quoted text clipped - 4 lines]
> > PS: does this mean that, I can not use the constructor to provide some
> > operations for all the subclasses?

Kevin,

 In Java, just like in C++, constructors are not typical functions.
One of the differences is that constructors are not inherited by
subclasses.  You can confirm that by trying to use c'tors from
non-abstract superclass not define in the subclass .
Patricia Shanahan - 24 Jul 2006 04:46 GMT
> abstract class A
> {
[quoted text clipped - 20 lines]
> I found the behavior of constractors are pretty different from normal
> functions. How to explain it?

Perhaps it is because constructors are not methods?

Constructors are not inherited. However, a subclass constructor can be
very short and simple:

public B(int i, int j){
  super(i,j);
}

If you want to ensure that every subclass has a constructor that deals
with i and j, get rid of the parameterless new A() constructor. It
implies that

public B(){
  super();
}

would be a valid B constructor, and that is what the compiler makes up
as default constructor if B does not specify at least one.

Patricia


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.