[Go to site: main page, start]

0% found this document useful (0 votes)
22 views4 pages

Understanding Java Constructors and Usage

Uploaded by

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

Understanding Java Constructors and Usage

Uploaded by

harinimar12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

CONSTRUCTORS

Purpose: To initialise an object


When the constructor code be used? Whenever a new object is created. so the number
of times a constructor is called is equivalent to the number of objects created.
All classes will have
constructor including abstract classes. What about interface? NO. why? See at end.
Syntax of constructor?
ClassName(Class variables) or ClassName()
{
this.variable1 = variable1;
this.variable2 = variable2;
--
}
Return type? Not Applicable for constructor (Even void is NA).
So what happens when u specify a return type? Compiler considers it as a
"method" instead.
If thats the case, can u have name of a method same as the class name (since
constructor has same class name)?
yes, it is valid but bad programming practice.

What happens if you dont specify a constructor for a class?


In that case, "compiler" (not jvm) provides a default constructor on its own.
But once a programmer has given a constructor then this default constructor wont be
generated.

Points on the default cosntructor generated by compiler:


This default constructor generated by compiler is "no args" constructor. that
is no arguments
The access modifier of the default constructor generated by the compile is
same as the access modifier of the class.
The only code inside the default construcot is super(). A no-arg call to the
SuperClass constructor. Class Test

Test()
//default constructor

Super();

}
Example:

Class AirMarket
{
String airlineCode;
int flightNo;
Airmarket(String airlinecode, int flightNo)
{
[Link] = airlineCode;
[Link] = flightNo;
}
public static void main(String[] args)
{
AirMarket a1 = new AirMarket("CM", 101); //creates a obj a1 with CM
and 101 as teh varialbes.
AirMarket a2 = new AirMarket("UA", 102); //creates a obj a1 with UA
and 102 as teh varialbes.
}
}

Modififers that can be used? Public, <default>, Private and Protected. (Anywhere
w/n or outside package, within package, within class, within class and child
classes respectively).
So why static and final can't be used? 1. Static members mean they belong to the
class itself with no instance creation needed while construtors is for instance of
a class.
So it breaks the
basic idea of constructor.
2. Final means it cannot
be subclassed if it is a class or it cannot be modified if it is a method.
A constructor cannot
be overriden by the subclasses since each class gets its own constructor.
So there is no need
to declare a constructor as final.

Other Rules of Constructors:


1. Constructor should always either "Super" or "this" as the first call in
it. Else compile error is thrown.
super() and this() are superclass constructors and current class
constructors.
This is different from super and this.

2. Overloading is acceptable in constructor. that is multiple constructor for


the same class with different parameters (both count of parameters and/or type) is
allowed.
3. But Overriding and Inheritance is not accepted for constructors.
4. when parent class contains a constructor with argument then the child must
also have one. Else what will happen if the child does not have a cosntructor with
smae argument is the child will then hve a defualt cosntructro created
by jvm which will have the super() call calling the no arg
constructor of parent class but since the parent class has a constructor with arg
only. So error.
so we need matching constructor between parent and child class.
Option will be if u write a cosntructor with arguments in the
parent clas, also write one constructor with no arg also so that no error happens.

Difference between super() and this(), vs super and this:


1. When u have same variable used in both parent and child. you have a method
in child class using the variable then by default child class variable will be
chosen.
If you want to use parent class variable in "child" method then use
Super method.
2. Super and this keywords can be used anywhere except static area (since
super and this is always related to obj). Super() and this() can be used only in
cosntructor.
3. Super and this keywords can be used any number of times in a method.
Super() and this() can be used only once that too in a constructor.

Class P
{
String s = "Parent variable";
}
Class C extends P
{
String s = "child variable";
public void m1()
{
Sopln(s); //child variable
Sopln(this.s); //parent variable
Sopln(super.s); //child variable
}
}

All classes will have constructor including abstract classes. What about interface?
NO. why?
For abstract classes sometimes instance variables can be present. To initialise
these variables, constructor can be used.
For Interface, every variable if present should be public static and final i.e.,
all initialisations should be done inside already.
For eg: u cannot have "int x" inside a [Link] will throw error.
So, since there is no instance variable constructor concept is not applicable
here.

Recursive constructor call:


when a constructor calls another constructor - recursive constructor invocation
which will be thrown as error by compiler
even though the constructor call doesnt happen since no object is created.

Class Test // Output will be recursive constructor error


{
Test()
{
this(10); // call constructor with integer. so goes to line 103
}
Test(int i);
{
this(); //calls constructor with no argument. so goes to line
99.
}
p s v main(String[] args)
{
Sopln("hello');
}
}

Now what will happen if the same recursion happens in methods? NO compiler error
but u end up with StackOverflow error on running with method call.
If the method is not called at all then no issues.

Class Test
{
p s v m1()
{
m2(); // call
}
p s v m2();
{
m1(); //
}
p s v main(String[] args)
{
m1(); -------------------------> if called then returns stack
overflw error when run.
Sopln("hello');
}
}

You might also like