what is constructer?what are the type of constructer with
example.
Answers were Sorted based on User's Feedback
Constructor is a same as method but it isnot exactly a
method . It has same name as a Class. It has access
specifier but not return type. Constructor is used to
create and initialize objects at runtime.
There are different types of constructors in different oop
languages.
e.g. Consider, java language-there are two types:-
1. Default
2. Parameterized.
Is This Answer Correct ? | 12 Yes | 1 No |
A constructer has same name as class .it does not have any
data type in a program we can only have one constructer
with a class name because they can be overloaded.if we dont
create one the programe creates one for us.
public class kool{
/** constructer
public kool(){
*/
}
}
Is This Answer Correct ? | 5 Yes | 5 No |
Answer / sunil prasad
Constructor is a specail type of mrthod.constructor name
should be same with the class name
There is defferent type of construtor:-
default constructor
parameter constructor
default construtor automatically call when we create
object of class.
Is This Answer Correct ? | 1 Yes | 1 No |
class Shiva
{
public Shiva() // it is a constructor
{
//code
}
}
class Mainclass
{
public static void main(String args[])
{
Shiva obj=new Shiva();
}
}
explanation: the code written in constructor is
automatically executed when we create object for the
class.no need to call the constructor/method again.
point to note:
constructor must not have any return type
Is This Answer Correct ? | 1 Yes | 1 No |
Answer / prasad
A constructor is a method.it used to custom intialization
for an object.constructor name and class name is same.
consructor should not return any value not even void also.
A constructor will Executed at the time of initialization of
an object.
Different types of constructors:
--------------------------------
1)default constructor
2)parametarized constructor
Ex:
class sum{
int a,b;//instance variables
sum(int a,int b){
System.out.printl("sum of two numbers="+(a+b));
}
}
class Example{
psvm(String s[])throws Exception{
sum s=new sum(10,10);
}
}
o/p:sum of two numbers=20
/*here the constructor will be executed at the time of
creating the object*/
Is This Answer Correct ? | 0 Yes | 2 No |
What is interceptors in java?
What is a dao layer in java?
What is meant by swing in java?
What is the java api?
What happens when you omit a brace or misspell one of the words, like public or
What is jndi datasource in java?
What is transient in java?
How do I open java console?
Describe the principles of oops.
1.can i use super keyword in normal class(not inheritance) to call any method?if so how can i call particular variable? 2.In the inheritance how can i access the particular variable from the base class(it containing 5 variables) using super keyword?
What is the content of the Java 2 security policy file?
What is exe file in java?