result management system report for college project
Constructors
1. Submitted to :-
MR. R.S.Rawat Sir
(H.O.D. of it department)
Submitted by:-
Puneet tiwari
B.e. viith sem
enroll no.:-0601it131034
2. contents
• What is Constructor.
• Properties of constructor.
• Difference B/w constructors and methods.
• Types of constructors.
• Constructor overloading.
• This();--constructor call
3. What is Constructor
• The constructor is a special method used to
initializing object.
• A block of code which has to be executed on
creation of object is defined inside constructor.
• It is used to initialize non static variables of a class.
4. Properties of constructor
• Constructor name is same name as class name.
• Constructor does not have return value not even
void.
• Constructors only allows access modifiers.
• It is called implicitly at time of creating object.
• It is called only once on one object.
5. Difference between constructors
and methods
• Method is called any number of times on the object.
• Constructor is called only once on object.
• Method is called explicitly.
• Constructor is called implicitly.
• Methods allow access and non access modifiers.
• It Allows only access modifiers.
• Method name can be any name.
• Constructor name must be same as class name.
• Methods are inherited.
• These are not inherited.
6. Types of constructors
Constructors
User defined
constructors
Non parameterized
constructors
Parameterized
constructors
Compiler defined
constructors
Default
7. 1. Compiler defined constructor
• If there is no constructors within a class compiler
provide default constructor without any
parameter this is called as default constructor.
• Default constructor access modifier is same as
class.
9. 2. User defined constructor
• Constructor defined by programmer is called the user
defined constructor.
I. Non parameterized constructor– A constructor
written in a class without any parameter is called as
non parameterized constructor.
This constructor does not receive any value.
10. Example
class A
{
A()
{
System.out.println(“Inside Constructor ”);
}
void A()
{
System.out.println(“Inside Method”);
}
}
Class Main
{
public static void main(String [ ] args)
{
A obj1=new A();
obj1.A();
}
}
Output –
Inside Constructor
Inside Method
11. II. Parameterized constructor- parameterized constructor is
user defined constructor.
• This constructor receive values at the time of creating objects
• This constructor allow two types of parameters.
i. Primitive types.
ii. Reference types.
• If constructor having primitive type it receive value.
• If constructor having reference type it receive address of object.
12. Example
class cons
{
private int no;
private String name;
cons(int no,String name)
{
this.no=no;
this.name=name;
}
void display()
{
System.out.println(no);
System.out.println(name);
}
}
class Main
{
public static void main(String [ ] args)
{
cons obj1=new cons(101,"Puneet");
obj1.display();
}
output-
101
puneet
13. Constructor overloading
• Defining more than one constructor within a class with same
name is called as constructor overloading.
• The constructors must be change in at least one criteria.
I. Number of parameters.
II. Types of parameters.
III. Order of parameters.
• Constructor is overloaded to extend functionality of existing
constructor.
14. Example
class Abc
{
Abc()
{
System.out.println("Non parameterized constructor");
}
Abc(int a,String s)
{
System.out.println(a);
System.out.println(s);
}
}
class Main
{
public static void main(String [ ] args)
{
Abc obj1=new Abc();
Abc obj2=new Abc(601,"Igec Sagar");
}
output-
Non parameterized constructor
601
Igec Sagar
15. Constructor call-using this();
• One constructor call another constructor of same
class using “this()” constructor call.
• It allows code reusability.
• It is used inside the constructor but not inside the
method.
16. Example
class Abc
{
Abc()
{
System.out.println("Non parameterized constructor");
}
Abc(int x)
{
this();
System.out.println("parameterized constructor");
}
}
class Main
{
public static void main(String [ ] args)
{
Abc obj1=new Abc(10);
}
}
Output--
Non parameterized constructor
Parameterized constructor
17. Java is like a sea if you are too much thirsty then only you can able to learn it as whole--.