0% found this document useful (0 votes)
5 views

program of constructor overloading

The document provides examples of different types of constructors in Java, including default, parameterized, constructor overloading, and copy constructors. Each example demonstrates how to create instances of classes using these constructors and outputs relevant information. The code snippets illustrate the functionality and usage of constructors in object-oriented programming.

Uploaded by

anukumarinaira03
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

program of constructor overloading

The document provides examples of different types of constructors in Java, including default, parameterized, constructor overloading, and copy constructors. Each example demonstrates how to create instances of classes using these constructors and outputs relevant information. The code snippets illustrate the functionality and usage of constructors in object-oriented programming.

Uploaded by

anukumarinaira03
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Default Constructor:

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;

Geek(String name, int id) {


this.name = name;
this.id = 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);
}

// constructor with two arguments


Geek(String name, int age)
{

System.out.println(
"Constructor with two arguments : "
+ " String and Integer : " + name + " " + age);
}

// Constructor with one argument but with different


// type than previous..
Geek(long id)
{
System.out.println(
"Constructor with one argument : "
+ "Long : " + id);
}
}

class GFG {
public static void main(String[] args)
{
// Creating the objects of the class named 'Geek'
// by passing different arguments

// Invoke the constructor with one argument of


// type 'String'.
Geek geek2 = new Geek("Shikhar");

// Invoke the constructor with two arguments


Geek geek3 = new Geek("Dharmesh", 26);

// Invoke the constructor with one argument of


// type 'Long'.
Geek geek4 = new Geek(325614567);
}
}

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();

// This would invoke the copy constructor.


Geek geek2 = new Geek(geek1);
System.out.println(
"Copy Constructor used Second Object");
System.out.println("GeekName :" + geek2.name
+ " and GeekId :" + geek2.id);
}
}

You might also like