Call constructor from another one?super class in subclass, JAVA Programming

2089_Untitled.png

 

What occurs if you do not give a constructor? Java does not actually need an explicit constructor in the class description. If you do not add a constructor, the Java compiler can create a default constructor in the byte code with an empty argument. This default constructor is same to the explicit “Pet(){}”. If a class add one or more explicit constructors like “Pet(){}” or “public Pet(int id)” etc, the java compiler does not prepare the default constructor “Pet(){}”.

 

How to call the superclass constructor in sub class? If a class called “SpecialSet” extends your “Set” class then you will use the keyword “super ” to invoke the superclass’s constructor. E.g.

 

public SpecialSet(int id)

{

super(id); //must be the very first statement in the other constructor.

}

To call a regular method in the super class need: “super .myMethod( );”. This may be called at any line. Some frameworks based on JUnit include their own initialization code, and not only do they need to remember to invoke their parent's setup() function, you, as a user, have to remember to invoke theirs after you wrote your initialization code:

 

public class UnitTestCase extends TestCase {

public void setUp() {

super .setUp();

// do class own initialization

}

}

public void cleanUp() throws Throwable

{

try {

… // Do stuff here to clean up your object(s).

}

catch (Throwable t) {}

finally{

super .cleanUp(); //clean up your parent class.

// super.regularMethod() can be called at any line.

}

}

 

Posted Date: 7/25/2012 12:40:16 AM | Location : United States







Related Discussions:- Call constructor from another one?super class in subclass, Assignment Help, Ask Question on Call constructor from another one?super class in subclass, Get Answer, Expert's Help, Call constructor from another one?super class in subclass Discussions

Write discussion on Call constructor from another one?super class in subclass
Your posts are moderated
Related Questions
what is methods override?

Project Description: The program must work on all realms, all ladders, all cores (4x realms,2x ladders,2x cores) The program must be unpatchable and undetectable If patche

Question : (a) Explain the concept of polymorphism when used in programs using suitable examples of your own. (b) (i) Distinguish between abstract methods and non-abstrac

Prepare Neo4j database of airline airports and routes. Implement Dijkstra algorithm to evaluate shortest path for flights with available seats. I'm need a developer with Neo4j/g

Differentiate between SMTP and POP3 server? SMTP (Simple Mail Transfer Protocol) is a protocol used to send and receive eMail messages over a TCP/IP network. While POP3 (Post


Ask question Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the superclass of the hierarchy. Create

• A Graph Data Type (including a display method to show adjacencies) • A Cycle Checker (as described in lecture notes) • An Edge class • A sorting algorithm to sort an array of Edg

Ask question #MBookshelves A library decides buy bookshelves to store its books and they need to determine the bookshelves’ heights and widths in order to minimize storage costs. T

have some assignments and i want help with them. thanks