when is class name and function name is same?
Answers
Answered by
2
*⃣❇❇❇ANSWER ❇❇❇*⃣
WHEN CLASS NAME AND FUNCTION NAME IS SAME ITS CONSIDERED AS CONSTRUCTOR✔✔ WHICH IS USE TO INITIALIZE THE VARIABLES EITHER WITH THEIR DEFAULT VALUES OR WITH OTHER VARIABLES.
EXAMPLE :---
import Java.io*;
Class pm
{
int a;
pm()
{
a=0;
}
}
✨✨✌✌HOPE IT HELPS ✌✌✨✨
WHEN CLASS NAME AND FUNCTION NAME IS SAME ITS CONSIDERED AS CONSTRUCTOR✔✔ WHICH IS USE TO INITIALIZE THE VARIABLES EITHER WITH THEIR DEFAULT VALUES OR WITH OTHER VARIABLES.
EXAMPLE :---
import Java.io*;
Class pm
{
int a;
pm()
{
a=0;
}
}
✨✨✌✌HOPE IT HELPS ✌✌✨✨
aryan7274:
example plz
Answered by
5
ANSWER :
A constructor has the same name as the class name ..
Whenever a function name is same that of the class name it has to be a constructor.
A constructor is generally invoked automatically and works with the motto of initialising the instance variables.
EXAMPLE OF CONSTRUCTOR
class rubbish
{
int n;
rubbish()
{
n=0;
}
void main()
{
System.out.println("Rubbish="+n);
}
}
------------------------------------------------------------
The constructor used is "rubbish() "
Hope you understand and here are the types of constructors :
(i) Copy constructors.
(ii) Default constructor ( eg : "rubbish()" )
(iii) Parameterized constructor.
Thanks !
Similar questions
English,
7 months ago
Political Science,
7 months ago
English,
1 year ago
India Languages,
1 year ago