CONSTRUCTOR
Ø It is special
member of a class that has following characteristics :
o It has the same name as of its class.
o It does not have an explicit return type.
o It is implicitly invoked just after an object is
created.
o It is used to initialize data members of objects.
Rectangle r = new Rectangle();
1. Rectangle r; //Reference
Variable created
2. r = new Rectangle(); //memory
allocation for object
3. r.Rectangle(5, 4); //Constructor
is invoked
Let’s see it again :
If a class doesn’t contain any constructor then default constructor is
provided by the compiler at the time of compilation but if a class contains any
parameterized constructor and there is an object of the class which requires
default constructor for its initialization then default constructor need to be
defined in the class. In this case, it won’t be provided by the compiler.
·
javap – to know what is kept in .class file
o Question: Why compiler
provides a default constructor if a class doesn’t contain any constructor and
why it doesn’t provide the default constructor when a class contains any
parameterized constructor?
o Solution
§ Overloading – If a class contains multiple implementations of a
method or constructor then method or constructor is said to be overloaded.
§ Implementations of overloaded method or constructor
are differentiated either by varying the number of arguments or by varying
their type of arguments.
§ Overloading is one of the means of implementing polymorphism.
o Let’s see the advantage of overloading –
- PROGRAM Test.java
- public class Test
- {
- public static void main(String[] args)
- {
- int a = 5;
- boolean b = false;
- char c = 'A';
- System.out.println(a);
- System.out.println(b);
- System.out.println(c);
- }
- }
OUTPUT
-
- class A
- {
- int a;
- public A(int x)
- {
- a=x;
- }
- {
- System.out.println("It is unusual....");
- }
- public void display( )
- {
- System.out.println("a=" +a);
- }
- public static void main(String[ ] args)
- {
- A p = new A(10);
- A q = new A(20);
- p.display();
- q.display();
- }
- }
https://saglamproxy.com
ReplyDeletemetin2 proxy
proxy satın al
knight online proxy
mobil proxy satın al
G02N3O