|
|
08.27.09 How To Build And Call Superclasses By Vaibhav PandeyInheritance is the core concept of Object oriented programing and Java programming Language. Since every subclass has exclusive rights to use the every resource of its superclass. Constructor of superclass holds important position in this regard. There are many functions a subclass can import by calling the constructor of its superclass. The call to the Superclass constructors can be made by using the ' super ' keyword. The main points to remembered in case of using super keyword are as follows:- 1. The default Constructor of the superclass is invoked even if the super() method is not called in the subclass. It is also invoked if you do not pass any arguments to super(). If the default constrictor is not available,compiler generates an error. 2. You can invoke a special constructor of the superclass by passing arguments to the super method,if the constructor is overloaded in the superclass. 3. The super() method must be the first statement in the constructor of the subclass. Lets take a look at following example:- The example illustrates how you can call a super class constructor. The Superclass class Car { Strring Name; int seats=4; Car(){ . . . . . }
Car(int seats,String Name){ . . . . } } Subclass which invokes the superclass constructor public class Subaru extends Car{ int wheels; public Subaru(){ super(4,"Subaru Impreza"); wheels=4; } } Here the constructor of superclass is invoked by the method call super(4,"Subaru Impreza") and you can call the default constructor by placing your call super(). By following the way you can invoke the superclass constructor which can be helpful in many tasks in your application. Comments About the Author: Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com. |
|
| ||
| -- ITProWire is an iEntry, Inc. publication -- iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509 2009 iEntry, Inc. All Rights Reserved Privacy Policy Legal archives | advertising info | news headlines | free newsletters | comments/feedback | submit article |