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

Some Java Topics

About Java programming language

Uploaded by

khamzada432
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)
3 views

Some Java Topics

About Java programming language

Uploaded by

khamzada432
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/ 11

Object Oriented Programming (OOP)

Lecture # 13

Dr. Asif Aziz Memon


Dept. of Computer Science
DUET, Karachi, Pakistan.
Java Methods

• 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.
• Why use methods? To reuse code: define the code once, and use it many times.

2
Void Keyword
• Allows us to create methods which do not return a value.
• Call to a void method must be a statement.

Example: Result:

3
Passing Parameters by Value
• While working under calling process, arguments is to be passed
• These should be in the same order as their respective parameters in the method
specification.
• Parameters can be passed by value or by reference.
• Passing Parameters by Value means calling a method with a parameter. Through this,
the argument value is passed to the parameter.

4
Passing Parameters by Value
• While working under calling process, arguments is to be passed

Example: Result:

5
Passing Parameters by Value
• While working under calling process, arguments is to be passed

Example: Result:

6
Method Overloading
• A class has two or more methods by the same name but different parameters, is
known as method overloading.

Example:

Result:
7
This Keyword
• this is a keyword in Java which is used as a reference to the object of the current class,
with in an instance method or a constructor.
• Using this you can refer the members of a class such as constructors, variables and
methods.
• The keyword this is used only within instance methods or constructors.

8
This Keyword
The keyword this is used to −
• Differentiate the instance variables from local variables if they have same names,
within a constructor or a method.

• Call one type of constructor (parametrized constructor or default) from other in a


class. It is known as explicit constructor invocation.

9
This Keyword
An example that uses this keyword to access the members of a class.

Result:

10
THANK YOU!

You might also like