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



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) {}


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
Deliverable:  1) WAR file for your application that can be deployed and run on TomcaT (War file must contains the Java source code) Requirement for application: 1. Application m

I want Selenium Based Test Automation for Java Based Web Application Project Description: I want some test cases to be automated to login to UI which is Java based web applic

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

In this assignment, you are provided with an interface that contains a generic type. You are asked to create two classes that implement this interface. A. The Sequenced Interfac

Normal 0 false false false EN-US X-NONE X-NONE

A JMS client that makes and sends messages.

How many kinds of errors can be found during a program development? List them. 1. Syntax errors 2. Semantic errors 3. Run-time errors

For what purposes FTP is used in networks? FTP is known as file transfer protocol it is basically used to download or upload files / data to a remote computer.

Explain Arithmetic Promotion and Binary Operations? An int divided through an int is an int, and a double divided by a double is a double, but what about an int divided by a do

Explain the Basic Applet Life Cycle ? 1. The browser reads the HTML page and seraches any tags. 2. The browser parses the tag to search the CODE and possibly CODEBASE att