0% found this document useful (0 votes)
60 views13 pages

Constructor Function: Constructor Function Is A Member Function of A Class Having

Constructor function is a special type of member function that is called automatically at the time of object creation. It initializes the object's data members and cannot be called like regular member functions. A constructor is called only once in the lifetime of an object. Common tasks performed by a constructor include initializing data members to default values.

Uploaded by

Pirzada Swati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views13 pages

Constructor Function: Constructor Function Is A Member Function of A Class Having

Constructor function is a special type of member function that is called automatically at the time of object creation. It initializes the object's data members and cannot be called like regular member functions. A constructor is called only once in the lifetime of an object. Common tasks performed by a constructor include initializing data members to default values.

Uploaded by

Pirzada Swati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

Constructor Function

• Constructor function is a member function of a


class having:
• Same name as class name
• No return type
• Is called automatically at the time of creation of an object
of the class.
• It cannot be called like other member functions
• Is called only once in the life time of an object i.e. at the
time of creation.
• Is normally used to initialize the data members of an
object.
Constructor Function
1. class demo
class Example 2. {
{ 3. Public static void main(String as[])
int x; 4. {
// the following function is constructor 5. Example ob1=new Example(); //
function constructor function called for ob1
automatically
Example()
6. Example ob2=new Example(); constructor
{ function called for ob2
x=0; 7. ob1.get(); // ouput x=0
} 8. ob2.get(); //outpit x=0
void set(int a) 9. ob1.set(50);
{ 10.ob2.set(30);
x=a; 11.ob1.get(); //output x=50
}
void get() 1. ob2.get); //output x=30
{ 2. }
System.out.println(“x=“ + x); 3. }
}
}
Constructor Function
1. Class demo
Class Example 2. {
{ 3. Public static void main(String as[])
int x; 4. {
// the following function is constructor 5. Example ob1=new Example(); //
function constructor function called for ob1
automatically
Example()
6. Example ob2=new Example(); constructor
{ function called for ob2
x=10; 7. Ob1.get(); // ouput x=10
} 8. Ob2.get(); //outpit x=10
void set(int a) 9. Ob1.set(50);
{ 10.Ob2.set(30);
X=a; 11.Ob1.get();
} 12.Ob2.get);
void get() 13.}
{ 14.}
System.out.println(“x=“ + x);
}
}
Constructor Overloading

• Two or more than two constructor functions having different


number of arguments or same number of arguments with
different data types is called constructor overloading
Class Example Void get()
{ {
int x; System.out.println(“x=“ + x + “y=“ + y);
double y; }
// the following function is no argument constructor }
function Class demo
Example() {
{ Public static void main (String as[])
X=0; {
} Example ob1=new Example(); // no argumnet
//the following function is one argument constructor function called for ob1
constructor functionwith int argument
Example(int a) Example ob2=new Example(20); one argument
{ constructor function with int data type called
X=a;
} Example ob3=new Example(2.9); one argument
//the following function is one argument constructor function withfloat data type called
constructor functionwith one float argument
Example(double a) Example ob4=new Example(20,3.6); two argument
{ constructor function called
y=a;
} Ob1.get(); X=0 y=0.0
//the following function is two argument Ob2.get(); X=20 y=0.0000
constructor functionwith one int and one float Ob3.get(); X=0 y=2.9
argument Ob4.get(); X=20 y=3.6
Example(int a, double b) }
{ }
X=a;
Y=b; }
Class Example Void get()
{ {
int x; System.out.println(“x=“ + x + “y=“ + y);
double y; }
// the following function is no argument constructor }
function Class demo
/*Example() {
{ Public static void main (String as[])
X=0; {
}*/ Example ob1=new Example();
//the following function is one argument Example ob2=new Example(50);
constructor functionwith int argument
Example(int a) Example ob3=new Example(60.8);
{
X=a; Example ob4=new Example(70,8.5);
} Ob1.get();
//the following function is one argument Ob2.get();
constructor functionwith one float argument
Example(double a) Ob3.get();
{ Ob4.get();
y=a; }
} }
//the following function is two argument
constructor functionwith one int and one float
argument
Example(int a, double b)
{
X=a;
Y=b; }
Function Overloading/Method
Overloading

• Two or more than two member functions in class with the


same name but different number of arguments OR
• same number of arguments with different data types is called
function overloading/ Method Overloading
1. class Example 1. void get()
2. {
2. { 3. System.out.println(“x=“ + x + “y=“ +
3. int x; y);
4. double y; 4. }
5. }
5. Example() 6. class demo
6. { 7. {
7. x=0; y=0.0; } 8. public static void main (String as[])
9. {
8. void set(int a) {
10. Example ob1=new Example();
9. x=a; 11. Ob1.get(); //X=0 y=0.0
10. }
11. void set(double b) 12. Ob1.set (10);
13. Ob1.get(); //X=10 y=0.0
12. {
13. y=b; 14. Ob1.set (2.5);
14. } 15. Ob1.get(); //X=10 y=2.5
15. void set(int a, double b)
16. Ob1.set (30,5.7);
16. { 17. Ob1.get(); //X=30 y=5.7
17. x=a; 18. }
18. y=b; } 19. }
Ob1
X
Y
Assignment
Teacher Vehicle
Name, Name,
Department, model,
Salary Price,
Gender company,
age
No argument constructor function
No argument constructor function Parameterized construction function
Parameterized construction function Display()
Display()

Student
Rollno,
Name,
Gender
Department,
semester
No argument constructor function
Parameterized construction function
Display()
Instance Variable Hiding (Use
of “this” Keyword)
• The data members and local variable of a class
may have the same name.
• This phenomena creates a conflict for accessing
the exact data variable.
• The local variable of the function gets
precedence.
• It can be said the local variable of a function
hides the data members of the class
• This key word is used to resolve conflict.
Instance Variable Hiding (Use
of “this” Keyword)
1. class Example{ 1. class demo
2. int x; // data member named x 2. {
3. Void set(int x) // the local 3. Public static void main (String as[ ])
varaibles of the function named 4. {
x 5. Example Ob1=new Example();
4. { 6. Ob1.set(30);
5. //x=x in correct assignment but 7. Ob1.get();
valid.
6. this.x=x; // this key word is 8. }
used to refer current object 9. }
7. } 10.Output:
8. Void get(){ 11.30
9. // no need to use this key word
here
10.System.out.println(“x=“ + x);
11.}}
Garbage Collection.
• Garbage collection (GC) is a form of automatic memory
management.
• The garbage collector, or just collector, attempts to
reclaim garbage, or memory occupied by objects that are no longer
in use by the program.
• A mechanism that periodically checks unreferenced objects in
memory.
• It deallocates the memory allocated to objects.
• Finalization
• Protected void finalize ()
• {
• //Code to be executed prior to the destruction of object of the class
• }
Finalization Example
1. class Example{

2. protected void finalize ()


3. {
4. //Code to be executed prior to the destruction of object of the
class
5. }

6. }

You might also like