943,945 Members | Top Members by Rank

Ad:
  • Java Code Snippet
  • Views: 3288
  • Java RSS
3

Constructors & inheritance

by on Oct 1st, 2009
I just wanted to demonstrate a quick point regarding Constructors & inheritance.
Look at these two classes and think about what you might expect to see after the main method has run.
Notice that ClassB is a subclass (inherits from) of ClassA. So, what do we expect the output to be? Here's the output:

run:
Class A constructor
Class B constructor
BUILD SUCCESSFUL (total time: 0 seconds)

As you can see, the main method makes an explicit call to ClassB's constructor, but not ClassA's constructor. ClassA's constructor, is, however called implicitly because ClassB is a subclass of ClassA. An important thing to note is that ClassA's consructor actually finishes executing BEFORE ClassB's constructor! Take a look at this code:
Java Code Snippet (Toggle Plain Text)
  1. ----------------------------------------------------------------------
  2.  
  3. public class ClassA
  4. {
  5. public ClassA()
  6. {
  7. System.out.println("Class A constructor");
  8. }
  9. }
  10.  
  11. ----------------------------------------------------------------------
  12.  
  13. public class ClassB extends ClassA
  14. {
  15.  
  16. public ClassB()
  17. {
  18. System.out.println("Class B constructor");
  19. }
  20.  
  21. public static void main(String[] args)
  22. {
  23. new ClassB(); // explicitly call ClassB constructor
  24. }
  25.  
  26. }
  27.  
  28. ----------------------------------------------------------------------
Comments on this Code Snippet
Oct 1st, 2009
2

Re: Constructors & inheritance

Because the first line of every constructor, is a call to a super constructor. If you do not add it yourself (i.e. call a specific super constructor) the compiler automatically adds a call to the default constructor of the super class. And, yes, obviously, this call will finish before the constructor in which it is to be found. To be complete your code is actually as follows:

Java Syntax (Toggle Plain Text)
  1. public class ClassA {
  2. public ClassA() {
  3. super(); // the default constructor of Object
  4. System.out.println("Class A constructor");
  5. }
  6. }
  7.  
  8. public class ClassB extends ClassA {
  9. public ClassB() {
  10. super(); // the default constructor of ClassA
  11. System.out.println("Class B constructor");
  12. }
  13.  
  14. public static void main(String[] args) {
  15. new ClassB(); // explicitly call ClassB constructor
  16. }
  17. }
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 1st, 2009
0

Re: Constructors & inheritance

Thanks for that input masijade, I have found that the call to super() is often omitted in these circumstances, and that explicitly calling super() is redundant, because it is called implicitly. It does, however, improve code readability to show our call to super() as the first statement. So, good point and thanks for viewing!
Last edited by majestic0110; Oct 1st, 2009 at 6:52 pm.
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 2nd, 2009
2

Re: Constructors & inheritance

That is what I said, that super() is added by the compiler, if you don't add a super call yourself. I simply illustrated what the code effectively was. In fact, if your classes looked like this
Java Syntax (Toggle Plain Text)
  1. public class ClassA {
  2. public ClassA(String s) {
  3. System.out.println("Class A constructor " + s);
  4. }
  5. }
  6.  
  7. public class ClassB extends ClassA {
  8. public ClassB() {
  9. System.out.println("Class B constructor");
  10. }
  11.  
  12. public static void main(String[] args) {
  13. new ClassB(); // explicitly call ClassB constructor
  14. }
  15. }

You will get the compiler message "Implicit super constructor ClassA() is undefined. Must implicitly invoke another constructor."
Last edited by masijade; Oct 2nd, 2009 at 2:48 am.
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 2nd, 2009
0

Re: Constructors & inheritance

That's great, thanks for your input on this snippet. The clearer it is to others, the better!
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Aug 10th, 2010
0

Re: Constructors & inheritance

I just wan to add one point -
If you define a constructor in a class, you must have to define the default constructor(without parameter) also, otherwise you will get an compilation error.


That's great, thanks for your input on this snippet. The clearer it is to others, the better!
Junior Poster
java_programmer is offline Offline
118 posts
since May 2006
Aug 11th, 2010
0

Re: Constructors & inheritance

I just wan to add one point -
If you define a constructor in a class, you must have to define the default constructor(without parameter) also, otherwise you will get an compilation error.
Sorry, but not if you don't want that the "default constructor" be used, or when it is not applicable. Such as when a class revolves around a specific instance variable and that a class that doesn't have it defined is "useless" and a default value makes no sense, than a "default constructor" also makes no sense, so you define a constructor with which that value can be set and do not define a "default constructor". That way, any class extending that class must explicitly call that super constructor, thereby keeping the "extended class" valid.

I.E. here is an example (a bit construed but valid)

Java Syntax (Toggle Plain Text)
  1. package ages;
  2.  
  3. public class Age {
  4. private int age;
  5.  
  6. public Age(int age) { this.age = age; }
  7.  
  8. public int getAge() { return age; }
  9. }
  10.  
  11. ---------------------------------------
  12.  
  13. package ages;
  14.  
  15. public class OldAge extends Age {
  16. private static final int minAge = 65;
  17.  
  18. public OldAge() {
  19. super(minAge);
  20. }
  21.  
  22. public OldAge(int age) throws NotOldEnoughException {
  23. super(age);
  24. if (age < minAge) throw new NotOldEnoughException();
  25. }
  26. }

See, the Age class needs an age, and what should be the default age? There is no sensible value, and an Age without an age value is also nonsensical, so there is no default constructor. Therefore, all constructors of OldAge must explicitly call the super(int) constructor.
Last edited by masijade; Aug 11th, 2010 at 5:50 pm.
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Message:
Previous Thread in Java Forum Timeline: Graphics2D Doesn't Draw on First Call?
Next Thread in Java Forum Timeline: RSS Parser





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC