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

Constructor

Uploaded by

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

Constructor

Uploaded by

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

Constructor :

===========

A constructor is a special member function that is used to initialize a newly created object and is
called just after the memory is allocated for the object. It can be used to initialize the objects at the time
of object creation.

Constructors will be called automatically at the time of object creation.

Syntax:

=======

class classname

member variable declaration;

classname(Arguments)

statements;

....

Rules :

1) Its name must equal with its class name

2) Don't specify the return type, even void also.

3) Avoid specifing private access specifier

4) Constructor can get the arguments

5) It can be overloaded.

Default Constructor : (Constructor with no arguments)


----------------------------

Ex:

class myclass

myclass ()

System.out.println("Welcome to constructor");

public static void Main(string arg[])

myclass m1,m2,m3;

m1= new myclass();

m2= new myclass();

m3= new myclass();

Note :

If there is no constructor in our class, then the system will generate the default constructor for the class
with no arguments and no statements.

Parameterized Constructor :

-------------------------------------

Constructor with argument is known as parameterized constructor.

Ex:

class car

{
String cname,comp; //member variable

float price;

int model;

car (String cn, String co, int m, float p ) //local variable

cname=cn;

comp = co;

model =m;

price=p;

void display()

System.out.println("\nCar name : " + cname);

System.out.println("Company : " + comp);

System.out.println("Model : " + model);

System.out.println("Price : Rs." + price);

public static void main(String arg[])

car c1,c2,c3;

c1= new car("Swift","Maruthi",2022,80000);

c1.display();
c2= new car("Tavera","Cheverlet",2022,80000);

c2.display();

c3= new car("Nano", "Tata",2022,120000);

c3.display();

Ex: 2

-----

class bank

String cus_name;

int acno;

float amount;

bank(String cname, int ano, float amt)

cus_name = cname;

acno = ano;

amount = amt;
}

public void display()

System.out.println("\n Customer name : " + cus_name);

System.out.println(" Account number: " + acno);

System.out.println(" Opening Balance : " + amount);

public static void main(String[] args)

bank b1, b2;

b1 = new bank("Darvin", 1322323, 100000);

b2 = new bank("Dani", 2322354, 500000);

b1.display();

b2.display();

Constructor Overloading :

----------------------------------

A class can have multiple constructor with different number of arguments or different types of
arguments.
Ex:

class product

int qty;

string pname;

float price;

product()

pname = "Exercise rope";

qty = 10;

price = 250;

product(string pn, int q, float p) // arguments (or) parameters -> local variables

pname = pn;

qty = q;

price = p;

product(string pn)

{
pname = pn;

qty = 0;

price = 0;

void display()

System.out.println("\nProduct name : " + pname);

System.out.println("Product qty : " + qty);

System.out.println("Product price : " + price);

static void Main(string[] args)

product p1 = new product("tennis bat",1,2000);

product p2 = new product();

product p3 = new product("skipping rope");

p1.display();

p2.display();

p3.display();

You might also like