0% found this document useful (0 votes)
16 views

#7 #8 Methods and Constructor

Uploaded by

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

#7 #8 Methods and Constructor

Uploaded by

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

METHODS IN JAVA

* 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

Since the code method


of
addition the numbers
of
both .
reused
we

"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
-

memod don't return


anything void return type is
,
.
used

Use class or
primitive data types as return type of
the .
method
-

name

* Method Name :

It should (or some kind action


of
-

be verb

should start with camel


follow
small lesser and case in case
of
-

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
-

Gets finished when reached to the end

We can also
stop method
by return' even
for void return
type
-
=> Types Of Methods : -

*
System Defined Methods : -

Methods which are


already defined and
ready to use in Jave
-

like Math .
sqrt)

Methods
* User
Defined
:

Methods which the create based the


programmer program
-

upon

necessity
* Overloaded Method :
-

More than one method with same name is created in same class

Overloaded Method
only gets differentiated based
arguments
-

on

so name should be same


, arguments should be
differend and

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 .

So when to declare method static :

Methods which do not


modify the state
of the
object can be
-

declared
Static .

Utility method which do not use


any
instance variable and compute
-

only on
arguments .

Example Factory design pattern


:

* Final Methods :

Final Memod overridden


cannot be in Java It is because
final
-

. so

method means its


implementation cannot be
changed If . child class

of
cannot its implementation then overridden
change no use .

* Abstract Memod
It is
defined only in
abstract class
-
.

declaration is done
Only method .
-

Its implementation is done in child classes


* Variable Arguments (Varargs) :

Variable number
of inputs
in the
parameter
-

.
the memod
-

Only one variable


argument can be
present in

should be last
It the
argument in the list
.
-

Used when don't


of
the number
we know
arguments
-

For Egi-
Variable

argument

So while
calling ,
we can
give multiple
no .

of arguments
CONSTRUCTORS
IN

JAVA

* What is constructor
?
-

It is used to create an instance(initialise the instance variable

-It's similar to method except :

·
Name : Constructor name is same as class name

Return Constructor do
·

Type : not have


any
return
type .

Constructor cannot be static


final abstract .
synchronized
·
or or
,

* 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
.

do not have return


*
Why constructor
type ?
-
There can be methods with same name and even class as return

but cannot be called do


type they
constructors as
they
not

rules in without return


obey of
the constructor same name

type

*
Why constructor cannot be
final ?
Constructors are
different from usual methods I cannot be in herited
-

So it doesn't make sense to make them


final because
final is used

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

constructor cannot be static


*
Why ?
Since static methods can
only static variables I other static
-

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 .

=> Type Of Constructors :

1)
Default Constructor
When we do not
define a constructor,
java internally provides
-

default constructor Default


constructor which is known as .

constructor also set


default values for all instance the

It is added
variables
.
only when we do not
define a constructor

2) No
Argument Constructor

If constructor that does not take . It is


any argument very
-

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-

another static method to create


ructor , we can create the
object
4) method
then call that
using class name
.

Constructor
=>
Chaining
* It means that we can call one constructor in other

This is done this 1) Super 11 To chain


using
constructor . .

a constructor within the same class this 11 is used


. For
eg
:
,
Here called other within
using
we constructor a constructor

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

Super 1) in child constructor then Java adds it


internally

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
·

You might also like