Você está na página 1de 10

Methods

ETL LABS PVT LTD – JAVA PROGRAMMING 132


What is Method?
A method is a block of code which only
runs when it is called. You can pass data,
known as parameters, into a method.
Methods are used to perform certain
actions, and they are also known as
functions.

Create a Method
4
A method must be declared within a
class. It is defined with the name of the
method, followed by parentheses (). Java
provides some pre-defined methods,
such as System.out.println(), but you can
also create your own methods to
perform certain actions:
Example
Create a method inside MyClass:

ETL LABS PVT LTD – JAVA PROGRAMMING 133


Call a Method
write the method's name followed by two
parentheses () and a semicolon; 3

In the example, myMethod() is used to print a


text (the action), when it is called:

ETL LABS PVT LTD – JAVA PROGRAMMING 134


What is Method Overriding?

If subclass (child class) has the same method as


declared in the parent class, it is known as method
overriding in Java.

Rules for Method Overriding


• The method must have the same name as in
the parent class
• The method must have the same parameter as
in the parent class.
• There must be an IS-A relationship
(inheritance).

ETL LABS PVT LTD – JAVA PROGRAMMING 135


Method Overloading
If a class has multiple methods having same
name but different in parameters, it is known
as Method Overloading.
3

If we have to perform only one operation,


having same name of the methods increases
the readability of the program.

ETL LABS PVT LTD – JAVA PROGRAMMING 136


Difference between method overloading and method overriding

Method Overloading Method Overriding


Method overriding is used to provide the specific
Method overloading is used to increase the readability of the
implementation of the method that is already provided by
program.
its super class.

Method overriding occurs in two classes that have IS-A


Method overloading is performed within class.
(inheritance) relationship.

In case of method overloading, parameter must be different. In case of method overriding, parameter must be same.

Method overloading is the example of compile time Method overriding is the example of run time
polymorphism. polymorphism.
method overloading can't be performed by changing return
type of the method only. Return type can be same or different Return type must be same or covariant in method
in method overloading. But you must have to change the overriding.
parameter.

ETL LABS PVT LTD – JAVA PROGRAMMING 137


What is this keyword?
There can be a lot of usage of this
keyword. In java, this is a reference
variable that refers to the current object.

Usage of java this keyword


• this can be used to refer current class
instance variable.
• this can be used to invoke current
4
class method (implicitly)
• this() can be used to invoke current
class constructor.
• this can be passed as an argument in
the method call.
• this can be passed as argument in the
constructor call.
• this can be used to return the current
class instance from the method.

ETL LABS PVT LTD – JAVA PROGRAMMING 138


What is this keyword?

There can be a lot of usage of this keyword. In java,


this is a reference variable that refers to the current
object.
Usage of java this keyword
• this can be used to refer current class instance
variable.
• this can be used to invoke current class method
(implicitly)
• this() can be used to invoke current class
constructor.
• this can be passed as an argument in the method
call.
• this can be passed as argument in the constructor
call.
• this can be used to return the current class instance
from the method.
ETL LABS PVT LTD – JAVA PROGRAMMING 139
What is Wrapper classes?

Wrapper classes provide a way to use


Primitive Data Type Wrapper Class primitive data types (int, boolean, etc..)
as objects.
byte Byte
The table below shows the primitive
short Short type and the equivalent wrapper class:
Example
int Interger
ArrayList<int> myNumbers = new
long Long ArrayList<int>(); // Invalid
ArrayList<Integer> myNumbers = new
float Float ArrayList<Integer>(); // Valid
double Double

boolean Boolean

char Character

ETL LABS PVT LTD – JAVA PROGRAMMING 140


Creating Wrapper Objects
To create a wrapper object, use the wrapper 3
class instead of the primitive type. To get the
value, you can just print the object:

ETL LABS PVT LTD – JAVA PROGRAMMING 141

Você também pode gostar