Constructors & inheritance

majestic0110 majestic0110 is offline Offline Oct 1st, 2009, 3:45 pm |
3
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:
Quick reply to this message  
Java Syntax
  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. ----------------------------------------------------------------------
1
masijade masijade is offline Offline | Oct 1st, 2009
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:

  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. }
 
0
majestic0110 majestic0110 is offline Offline | Oct 1st, 2009
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.
 
2
masijade masijade is offline Offline | Oct 2nd, 2009
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
  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.
 
0
majestic0110 majestic0110 is offline Offline | Oct 2nd, 2009
That's great, thanks for your input on this snippet. The clearer it is to others, the better!
 
 


Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC