This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructors and inheritance  RSS feed

 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If subclass has constructor for example...

public class X extends Y {


         X (String a, String b){

         }



}


public class Y {


       X (String x){

      }

      X(String x, String y, String z){

      }


}

When you create object of sublass which constructor will be called for superclass?
 
Carey Brown
Bartender
Posts: 2700
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In class Y you cannot have constructors called X. You can have a Y constructor that calls an X constructor.

AND, clean up your code before posting it next time you have way too many blank lines.
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When creating a subclass you call the constructor of the super class as follows:



When you are creating the subclass you call its constructor, not the super class. When you create the subclass the super has to be created anyways.

Just remember when creating an object of the subclass to include in its parameters the parameters of the super class's constructor.

If I was not clear let me know, I can explain better in great detail and use examples.

 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yosuf Ibrahim wrote:When creating a subclass you call the constructor of the super class as follows:  . . . I can explain better in great detail and use examples.


what happens then if super class has 2 or 3  constructors? What one will you supply to the constructor of the  subclass?
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:. . . what happens then if super class has 2 or 3  constructors? What one will you supply to the constructor of the  subclass?


Remember you cannot create 2 constructors that have identical parameters, therefore the one used will be the one whose parameters you defined. For example



As you see above I created the super class Animal with 3 constructors, and I created a subclass Dog with only 1 constructor. The Dog's constructor knows that I am trying to use the first constructor in the super class because of the signature. There is only one constructor that  takes only a String (I cant make 2 anyways, won't compile), so it uses that.

Cheers

lemme know if I can help more
 
Stribor Kab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yosuf Ibrahim wrote:. . .
lemme know if I can help more


how do you decide which constructor to use? Based on your needs?
 
Henry Wong
author
Sheriff
Posts: 23260
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:
how do you decide which constructor to use? Based on your needs?


The Animal class is *your* class. You wrote it. Or at least, supposed to be as you are the designer/programmer. And your Dog class subclasses from it. If you don't know which constructor to use, how will anyone here know? ...

Henry
 
Yosuf Ibrahim
Ranch Hand
Posts: 87
2
Chrome Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:how do you decide which constructor to use? Based on your needs?


Of course, based on your needs you make the constructors and based on them you use the one you want. In my current project, I have 1 constructor per class because that is all I need. In previous apps, I had up to 5 constructors in some cases. It totally depends on what you will be using it for.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first statement of a constructor must be the invocation of a constructor either of the same class, or of the super class. (Besides java.lang.Object)
If you do not explicitly invoke any constructor (using this() or super()), the compiler will insert an implicit call to the default constructor (that is, one without arguments) of the super class and it is an error if none is available.
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you explicitly define default constructor in superclass you can create any constructor in subclass.
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spencers wrote:If you explicitly define default constructor . . .
Despite what many people tell you, it is impossible to define a default constructor. Look in the Java┬« Language Specification. You have not written a default constructor there. You shou‍ld be able to see that the default constructor example there allows the two fields to have their default values, which is probably not what you want, so that is an example of bad design.
This link about the javadoc tool tells you it is bad style to allow default constructors.
 
Campbell Ritchie
Marshal
Posts: 54886
155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:. . . the default constructor (that is, one without arguments) . . . .
That is not what default constructor means.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!