#7 #8 Methods and Constructor
#7 #8 Methods and Constructor
* What is method
?
Method is used to
perform certain task
-
Collection
of
instructions performs
that
specific task
a
-
It code
be used to
bring the
readability $ee-usability
-
can
For
eg :
-
>
- Instruction I
Instruction &
>
-
>
-
Instruction 3
Here
Sum is method which will take I return the
arguments
a
"so it provides
reusability
to add
perform .
* How to declare method
?
Return
Type McMod
Access Method
Arguments
Specifier ↑
Name
↑ -
public int Sum (inta in + b) throws Exception [
,
Il method
body
3
* Access
Specifiers
Defines accessibility of
method i. e who the method
-
a can use
-
There are
types of
4 access
specifiers in Java methods :-
1) Public be accessed in
: can
through any
class
any package
.
Private
by methods only
2) : can be accessed in the same class
3) Protected : be accessed
by in
can other classes same
package or other
sub classes
different package
in .
4)
Default : It can
only accessed
be
by classes in same package .
If we do not mention
anything
then
Default specifier
,
access
is used
by Java
.
* Return Type
It tells what method will
type the return
after computation
.
If
the
-
Use class or
primitive data types as return type of
the .
method
-
name
* Method Name :
be verb
multiple words
* Method Parameters
list variables that will be used in the method
of
-
It's a .
-
Parameter list can be blank too
.
Method
*
Body :
'return
Method
body get finished when you
call in .
mid
-
We can also
stop method
by return' even
for void return
type
-
=> Types Of Methods : -
*
System Defined Methods : -
like Math .
sqrt)
Methods
* User
Defined
:
upon
necessity
* Overloaded Method :
-
More than one method with same name is created in same class
Overloaded Method
only gets differentiated based
arguments
-
on
is considered
.
return
type not even
* Querridden Memod
Subclass / Child Class has the same method class
the
parent
-
as .
* Static Memods :
-
These methods are associated with the class
Can be called
just with class name
.
-
-
Static methods can not access Non Static Instance variables and
methods
-
Static methods cannot be overridden .
declared
Static .
only on
arguments .
* Final Methods :
. so
of
cannot its implementation then overridden
change no use .
* Abstract Memod
It is
defined only in
abstract class
-
.
declaration is done
Only method .
-
Variable number
of inputs
in the
parameter
-
.
the memod
-
should be last
It the
argument in the list
.
-
For Egi-
Variable
argument
So while
calling ,
we can
give multiple
no .
of arguments
CONSTRUCTORS
IN
JAVA
* What is constructor
?
-
·
Name : Constructor name is same as class name
Return Constructor do
·
* new
Keyword tells java to call constructor
is same
*
Why constructor name as
of class name
?
because it is
Constructor name is
always same as class name
easy
-
adds
to
identify there is no return
type because
implicitly jave
type
class as return
.
type
*
Why constructor cannot be
final ?
Constructors are
different from usual methods I cannot be in herited
-
to
prevent overridingI f constructors cannot be inherited then
There is no requirement for final
constructor cannot be abstract?
Why
*
Since
for responsibility
abstract method its
the
implementation
of g
-
. But constructors
child class can't be inherited
even so no
point of
making them ·
abstract
access
methods ,
so it won't be able to initialise the instance variable
.
won't
We also be able to use constructor
I
chaining
call
super
11.
define
* Can constructor in
we
interface ?
No
of
because cannot create
object point constructor
-
we so no .
1)
Default Constructor
When we do not
define a constructor,
java internally provides
-
It is added
variables
.
only when we do not
define a constructor
2) No
Argument Constructor
similar te
default constructor but we are
defining
it instead
of
java .
3) Parameterised Constructors
It takes and the instance variables with those
arguments assign
-
parameters
. We can initialise one or multiple instance variables
using a
parameterised constructor
. For the variables where we don't provide
default
be instantiated with values
any argument they'll
,
4) Constructor overload
constructor with
-
We can create multiple different parameters .
5) Private Constructor
We create constructor outside the class will
can a
private no one
-
. This is used
be ablater call the constructor
usually in
Singleton
To
design pattern . create on
object of a class
having private cost-
Constructor
=>
Chaining
* It means that we can call one constructor in other
this ()
*
Using Super 1
The child class
of
invokes the constructor
constructor a
always
-
of parent first
class then invokes its own constructor
.
This don't
is done
using Super 1) ,
so
if we
explicitly add
for
So this 1) I 1) used
Super are constructor
chaining .
*
If the parent class has a
parameterised constructur when we'll ,
have to
mandatorily pass an
argument to super 11 to call the
parent class's parameterised constructor
·