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
Requested figure handle in use by another object, how do i fix this?

#comparison of merge sort and bubble sort

This project focuses on demonstrating the understanding of data types, input/output, sequential and selection programming statements, and mathematical operations. The project re

I am preparing ehopsital project. For the needs of hospital in that billing module and patient registration and stores module and e.t.c module is there for hospital needs. Skill

code for error correction and detection using even odd parity

Employee application Project Description: Subsequent Technology should be used 1) UI (jsp, jstl ,html, etc) 2) Framework (Spring MVC and hibernate) 3) Database (MYSQ

Project Description: I wish to prepare a Web server which provides subsequent Web Services. 1. User login 2. User input data store to server 3. User fetches results fro


Develop an E-commerce website I prefer this website to be coded with JAVA since I have heard this is safer than PHP is. Though, if this is not possible I am willing to go with P

REQUIREMENT: We are seeking a simple tool that will convert an existing flash file (.SWF) into HTML5. Our purpose is to convert small flash games into html5 games. Please NOTE: We