answersLogoWhite

0


Best Answer

These are two different things.

"Super" is used in inheritance - if class B inherits from class A, and we want the constructor of B to do the same things the constructor of A does (and maybe more things), we can call A's constructor in B's constructor:

public B() {

super();

......

}

Notice that it must be the first line in B's constructor.

"This" is used in case of overloading, to call a function aith the same name.

For example, if we have a constructor that receives one argument, and we it want to have a default value of 0, we can write:

public C() {

this(0);

}

User Avatar

Wiki User

โˆ™ 2011-09-15 11:45:44
This answer is:
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
User Avatar
Study guides

Mixture

1 card
โžก๏ธ
See all cards
5.0
โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…โ˜†โ˜…
1 Review

Add your answer:

Earn +20 pts
Q: What is use of super and this constructor in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


What is use of constructor in java?

Constructor is used to do something (written in constructor) immediately after object creation.


Why constructor use in java?

To create objects of classes


Does Java support copy constructor?

No. Java does not support copy constructor


What does the Java keyword super mean?

super keyword is used for two reasons: 1. To Call the super class or base class constructor 2.To call super class methods. If you don't create constructor in a class...Java automatically put for you. Always remember..that the first statement in derived class constructor must be a call to super or this.


Why we use super in java?

super is a keyword in java.uses of super are: 1.it is used to call super class constructor. 2.It is used to call a method of super class that is hidden by sub-class.


What is Default constructor?

The default constructor is an empty (only call the super constructor) with no parameters constructor inserted by the java compiler when you don't define a constructor in your class. If you write something like this: public class NoConstructorClass{ //no constructor goes here } Then you get something like this: public class NoConstructorClass{ public NoConstructorClass(){ // Default constructor that you didn't write super(); } }


What is use of private constructor?

A Private constructor is simply a constructor that can only be used within the class it was declared in. By default java creates you a constructor which is public though. Hope that helps


When does the compiler supply a default constructor for a class in java?

When the programmer has not coded a constructor for any java class.


Is it mandatory to use constructor in a class?

In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.In the case of Java, not always. The class does require a constructor, but the compiler will automatically include an "empty constructor" (constructor without parameters) under certain conditions.


How do you initialize a variable in java?

by the use of constructor . we can initialize the variables in java.The name of the constructor should be same as the name of the class .


Why super constructor should be first statement in subclass constructor in java?

Because, the parent class also needs to be initialized when you create an object in the inheritance hierarchy.


What is non parameterized constructor in java?

It is also called the default constructor. It is the constructor that the java compiler places in every class where the programmer has not explicitly coded a constructor. Ex: public class Car { ... //lots of code but no constructor } If a person writes a class as above, then the compiler places this code in the compiled version of the file. public Car() { super(); } This is called the non-parameterized or default constructor


How many types constructors are there in java?

Default Constructor Parameterised Constructor Non Parameterised Constructor


Is there a sample program for constructor?

All Java programs would have a constructor... public class Test { public Test(){ ... } ..... } This is a constructor. Even if you dont code the constructor Java would automatically place a default constructor for compilation.


Is there any alternative to constructor in Java?

No. The constructor is the starting point of creation of any object in Java. Without going through a constructor you cannot create an object


Why constructor in java?

What are the importance of constructor in java? Constructor is automatically called immediately after the object is created , before the new operator complete . Constructor is important because they have no return type , not even void.


What is a constructor and its mandatory to use constructor in a class?

A constructor is a method that is invoked when an object is created. As to being mandatory, that really depends on the programming language; in the case of Java, each class must have a constructor, however, in many cases Java will automatically provide a default constructor, so you don't really need to program it.


Can you use constructor in servlet?

Yes you can but it is not required. A Servlet is nothing but another .java file and all rules that are applicable to standard Java classes are applicable to them. Note: Even if you write a constructor in a servlet, it will not get executed.


Can a constructor be defined within a method in java?

No.


How you create a java constructor?

When we want to create an Object for a class at that time we will write constructor.


Why am I getting Java unreported exception while compiling my class?

It may be because you are using inheritance and your super class constructor has a throws clause in its declaration. If your super class has a throws clause in its constructor declaration, you must do the same in your child class constructor in order to eliminate this compiler error.


What is meant by constructor in java?

It is the way to create the instances of class. in java. to know how constructor works first you should be clear with the concept of class.


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


How do we Invoking Java constructor?

Constructors in java cannot be invoked explicitly. They are invoked automatically when an object is created. In java if u dont write any code for constructor, by default compiler inserts a zero argument constructor. If required, it can be overrided.