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
Consider the following Java definition of an integer list class. class IntegerList { private int[] list = new int[200]; private int size = 0; public boolean append(int value) {

What does WAN stand for? Wide Area Network - A network in that computers are separated through great distances classically across cities or even continents.

How to Implementing Interfaces in java? To actually utilize this interface you create a class that involves a public double calculateTariff() method and declare in which the cl

What do you understand by private, protected and public? These are accessibility modifiers. Private is the most restrictive, whereas public is the least restrictive. There is n

Video course on computer security needed Project Description: We want a video course on computer security. Just like the way lynda provides. Skills required are Computer S

Distributed / Network Oriented Java is network friendly -- both in its portable, threaded nature, and since common networking operations are built-in to the Java libraries.

What are the Sub categories of Artificial Intelligence? Briefly explain any two? 1. Expert systems: Expert system is in which system that is in some limited sense, could repl

What is an abstract class? Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not


Ask1. Storing the Array: Write an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a mes