| | |
Constructors & inheritance
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:
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:
---------------------------------------------------------------------- public class ClassA { public ClassA() { System.out.println("Class A constructor"); } } ---------------------------------------------------------------------- public class ClassB extends ClassA { public ClassB() { System.out.println("Class B constructor"); } public static void main(String[] args) { new ClassB(); // explicitly call ClassB constructor } } ----------------------------------------------------------------------
1
•
•
•
•
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)
public class ClassA { public ClassA() { super(); // the default constructor of Object System.out.println("Class A constructor"); } } public class ClassB extends ClassA { public ClassB() { super(); // the default constructor of ClassA System.out.println("Class B constructor"); } public static void main(String[] args) { new ClassB(); // explicitly call ClassB constructor } }
0
•
•
•
•
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
•
•
•
•
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
You will get the compiler message "Implicit super constructor ClassA() is undefined. Must implicitly invoke another constructor."
Java Syntax (Toggle Plain Text)
public class ClassA { public ClassA(String s) { System.out.println("Class A constructor " + s); } } public class ClassB extends ClassA { public ClassB() { System.out.println("Class B constructor"); } public static void main(String[] args) { new ClassB(); // explicitly call ClassB constructor } }
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.
Similar Threads
- again having problem about inheritance please help (C++)
- Derived Constructors Question (C++)
- has-a inheritance (C++)
- Question about Inheritance/Classes (C++)
- need idea for project using classes and inheritance (C++)
- C++ Inheritance Question (C++)
- ":" other than inheritance? (C++)
- Zend PHP Certification (PHP)
- accessing private data members (C++)
- Quick Polymorphism Tutorial (C)
| Thread Tools | Search this Thread |
.net 6 actuate addball ajax apple applet array automation beginner binary binarytree birt block busy_handler(null) c++ c/c++ class classes code collision component constructor convert design development dragging dynamic eclipse eclipsedevelopment error firefox forms fractal functiontesting game google gui guitesting homework html ide image infinite inheritance innodb integer intellij j2seprojects java javafx javascript jetbrains julia linux login loop looping loops method microsoft myaggfun mysql netbeans newbie news nextline opensource os pearl php print problem programming project python radio random rsa set smart software sort sortedmaps sourcelabs spring springsource staticcodeanalysis subclass sun superclass swing text-file thread threads tree variablebinding web windows wxpython




