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

OOPlecture 3 - Default and Parameterized Constructor

Uploaded by

nazia.majeed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOPlecture 3 - Default and Parameterized Constructor

Uploaded by

nazia.majeed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Constructors

Object Creation Syntax

DMA=Dynamic Memory Allocation


Constructor
Constructor is special type of method whose name is same as class name.

1) Main purpose of constructor is to


initialize the object.
2) Every java class has a constructor
3) A constructor is automatically called
at the time of object creation.
4) A constructor never contain any
return type including void

OUTPUT
Constructor
Constructor is special type of method whose name is same as class name.

1) Main purpose of constructor is to


initialize the object.
2) Every java class has a constructor
3) A constructor is automatically called
at the time of object creation.
4) A constructor never contain any
return type including void

OUTPUT
Type of Constructors
1) Private Constructors
2) Default Constructors
3) Parameterized Constructors
4) Copy Constructors
Default Constructor
• A constructor which does not have any parameter is default
constructor.
Default Constructor
Parameterized Constructor
A constructor through which we can pass one or more parameters is
called parameterized constructor
Syntax:
class A
{
A(int x, string y)
{
}
}
Parameterized Constructor
Class Task:
Create a Car class and its default constructor that will initialize the no of
tires and seating capacity in car.
no of tires will be 4 and seating capacity will be 5 for all car objects.

Create another constructor in Car class that will give different colors
and engine capacity to every car according to parameter passed.

You might also like