Programmer Coding

Creaters

Creater in java is a specific type of way(method) that is used to institute the object.
Java creater is produce at the time of object creation. It constructs the values i.e. gives datafor the
object that is why it is known as creater.
There are basically two rules circle for the creater.
1. Creater name must be same as its class name.
2. Creater must have no definitive return type.
Types of java creaters
There are two types of creaters.
1. Default creater (no-arg creater)
2. Constant limitation creater

 

Java Default Creater

A creater that have no Constant limitation is known as default creater.
Syntax of default creater:
1. <class_name>(){}
Example of default creater
In this example, we are creating the no-arg creater in the class. It will be produce time of
object creation.
class programercoding{
programercoding1(){System.out.println(“programercoding is produced”);}
public static void main(String args[]){
programercoding b=new
programercoding ();
} }

Output: programercoding is generated

Example of constant limitation creater

In this example, we have produced the creater of Coding class that have two parameters. Wecan
have any number of parameters in the creater.
class programercodeing{
int id;
String name;
programercodeing (int
i,String n){id = i;
name = n;
}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
programercodeing s1 = new
programercodeing (111,”Rawan”);
programercodeing s2 = new
programercodeing (222,”Ram”);
s1.display();
s2.display();
} }
Output:

111 Ravan
222 Ram

 

Creater Overloading in Java

Creater overloading is a technique in Java in which a class can have any number of creaters
that differentiates in parameter lists.The compiler differentiates these creaters by taking into
account the number of parameters in the list and their type.
Example of Creater Overloading
class coding4{
int id;
String name;
int age;
coding5(int i,String n){
id = i;
name = n;
}
coding5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+” “+name+” “+age);}
public static void main(String args[]){
coding5 s1 = new coding5(111,”Ravan”);

coding5 s2 = new coding5(222,”Ram”,25);
s1.display();

s2.display();
} }

output

111 Rawan 0
222 Ram 25

 

Java Copy Creater

There is no copy creater in java. But, we can copy the values of one object to another.
There are a lot of way(method) s to copy the values of one object into another in java. They are:
1. By creater
2. By assigning the values of one object into another
3. By clone() way(method) of Object class
In this example, we are going to copy the values of one object into another using javacreater.
class coding6{
int id;
String name;
coding6(int i,String n){
id = i;
name = n;
}
Coding6(coding6 s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
coding6 s1 = new coding6(111,”Rawan”);
coding6 s2 = new coding6(s1);
s1.display();
s2.display();
} }

output

111 Rawan
111 Rawan

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top