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
We want the best java programmer available. Project Description: Java | Portlets| JSF Liferay (not essential but is preferable) Big Data and User online tools. Multilin

Q. Write the method to define the packages in java programme. Explain. Ans. Package: When we work on a project we have to break our programme in several classes. To organize


The Action Class is type of the Model and is a wrapper around the business logic. The purpose of Action Class is to change the HttpServletRequest to the business logic. To use the

You may call function from any other place into your JavaScript code. After the function is executed, the control goes back to other script which called it.  alert('Example 1: t

In Java not all classes must be described separate from each other. You may put the definition of one class under the definition of another class. The class inside class is called

Talent agencies like ICM, CAA and Paradigm represent writers, directors and actors (among other talent).  These agencies take in hundreds of millions of dollars a year collecting 1


What is meant by semantic error? Occur while a statement executes and has an effect not intended through the programmer and Frequently times occur only in unusual & infrequent

The File class encapsulates the files and directories of the local file system. The RandomAccessFile class gives the methods required to directly access data contained in any part