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 / November 2007

Tip: Looking for answers? Try searching our database.

Constructor methods from extended classes

Thread view: 
larkmore@aol.com - 08 Nov 2007 19:01 GMT
I think I already know the answer, but want to double check.  If I
have two classes defined:

public class A {
 public A() {
   ...
 }

 public A(int i) {
   ...
 }
}

public class B extends A {
 public B() {
   ...
 }
}

Is there any way to prevent a programmer from calling
 B b = new B(10);
other than documentation saying please don't?  Are all methods
inherited by B from A always accessible to the programmer?

-Will
Daniel Pitts - 08 Nov 2007 19:27 GMT
> I think I already know the answer, but want to double check.  If I
> have two classes defined:
[quoted text clipped - 21 lines]
>
> -Will

Constructors aren't actually methods, and B doesn't inherit A(int i)

On the other hand, B() can chain to either A() or A(int i):

public B() {
   super(10); // like "new A(10)"
}

public B(){
   super(); /* Not technically needed. explicitly called if no other

               super() is provided */
}

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Are Nybakk - 08 Nov 2007 19:57 GMT
>> I think I already know the answer, but want to double check.  If I
>> have two classes defined:
[quoted text clipped - 34 lines]
>                super() is provided */
> }

To clarify, "super" is then a reference to the class' superclass.

Another example:

public B(type param1, type param2) {

   //call superclass' constructor to initialize inherited properties
   super(param1);

   //do initialization of B class properties
   ownParam = param2;

}
Daniel Pitts - 08 Nov 2007 20:02 GMT
>> public B() {
>>    super(10); // like "new A(10)"
[quoted text clipped - 18 lines]
>
> }

Actually, in this case super is a keyword that tells the compiler that
you wish to invoke a specific version of the constructor in the parent
class.  Similar to the this() constructor chaining.

Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roger Lindsjö - 08 Nov 2007 19:37 GMT
> I think I already know the answer, but want to double check.  If I
> have two classes defined:
[quoted text clipped - 19 lines]
> other than documentation saying please don't?  Are all methods
> inherited by B from A always accessible to the programmer?

Yes, it is prevented as constructors are not inherited. So, if you don't
define a constructor in B that takes an int, then new B(10) is not possible.

//Roger Lindsjö
larkmore@aol.com - 08 Nov 2007 19:45 GMT
> Yes, it is prevented as constructors are not inherited. So, if you don't
> define a constructor in B that takes an int, then new B(10) is not possible.

Well bust my buttons!  I always thought that constructors were
inherited.  Works out for me well for me that they aren't.  I guess
you learn something new everyday.  Thanks guys.  :)
-Will


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



©2009 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.