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 |
How do I download and install eclipse on windows 10?
What is interceptors in java?
What is the point of lambda expressions?
What is repository pattern in java?
What is aop in java?
How do I enable java in firefox?
What is custom tag in java?
Topic- looping,function overloading,nesting ,polymorphism. Aim - to write a function with a name buzz-buds,that will check whether the given numbers are buddies or not on the basis of no. of parameters passed during function calling.
can a program use more than one command-line argument?
How do you create a jar file?
why java has the size of character 2 byte ,while in c/c++ it is of 1 byte?
How can I protect myself? : java security