program of constructor overloading
program of constructor overloading
import java.io.*;
// Driver class
class BCA {
// Default Constructor
BCA() { System.out.println("Default constructor"); }
// Driver function
public static void main(String[] args)
{
BCA hello = new BCA();
}
}
Parameterized Constructor:
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
class GFG
{
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
}
}
Constructor Overloading:
import java.io.*;
class Geek {
// constructor with one argument
Geek(String name)
{
System.out.println("Constructor with one "
+ "argument - String : " + name);
}
System.out.println(
"Constructor with two arguments : "
+ " String and Integer : " + name + " " + age);
}
class GFG {
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments
Copy Constructor:
import java.io.*;
class Geek {
// data members of the class.
String name;
int id;
// Parameterized Constructor
Geek(String name, int id)
{
this.name = name;
this.id = id;
}
// Copy Constructor
Geek(Geek obj2)
{
this.name = obj2.name;
this.id = obj2.id;
}
}
class GFG {
public static void main(String[] args)
{
// This would invoke the parameterized constructor.
System.out.println("First Object");
Geek geek1 = new Geek("Avinash", 68);
System.out.println("GeekName :" + geek1.name
+ " and GeekId :" + geek1.id);
System.out.println();