0% found this document useful (0 votes)
5 views23 pages

Section 2-2

The document provides an overview of Java methods and object-oriented programming (OOP) concepts. It explains how to create and call methods, pass parameters, and return values, as well as the structure of classes and objects in OOP. Additionally, it highlights the advantages of OOP over procedural programming and includes examples demonstrating method usage and class attributes.

Uploaded by

fathiabdalrahem
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)
5 views23 pages

Section 2-2

The document provides an overview of Java methods and object-oriented programming (OOP) concepts. It explains how to create and call methods, pass parameters, and return values, as well as the structure of classes and objects in OOP. Additionally, it highlights the advantages of OOP over procedural programming and includes examples demonstrating method usage and class attributes.

Uploaded by

fathiabdalrahem
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

Advanced

Programming
Language
(AIE )

Section 2
Eng. Mohamed Ibrahim

Assist. Prof. Samah Adel


Assoc. Prof. Hatem Khater
2025-2026 Eng. Aya El Sayed
Java Methods
• A method is a block of code which only runs when
it is called to perform certain actions.
• They are also known as functions
• You can pass data into a method , known as
parameters, (inputs of a method)
• You can also receive data from a method , known
as return values, (outputs of a method)
Creating a method
• A method must be declared within a class.
• It is defined with the name of the method,
followed by parentheses ()

• myMethod
the name of the method

• static
means that the method belongs to the Main class and not

an object of the Main class.

• void
means that this method does not have a return value.
Calling a method
• To call a method, write the method's name followed by two parentheses () and a
semicolon ;
public class Main {

static void myMethod() {


[Link]("welcome to Java");
}
public static void main(String[] args) {
myMethod();
}

• A method can also be called multiple times


Method parameters

• Information can be passed to methods as parameter. Parameters act as


variables inside the method.
• Parameters are specified after the method name, inside the parentheses.
• You can add as many parameters as you want, just separate them with a
comma.
Method parameters
Example has a method that takes a String called name as parameter. When the method is
called, we pass along a name, which is used inside the method to print it.

public class Main {

static void myMethod(String name) {


[Link]("my name is " + name);
}
public static void main(String[] args) {
myMethod("ali");
myMethod("aya");
}

}
Multiple parameters
• You can pass many parameters to a method

public class Main {

static void myMethod(String name , int age) {


[Link]("my name is " + name);
[Link]("my age is " + age);
}
public static void main(String[] args) {
myMethod("ali", 18);
}

}
Return Values
• The void keyword, used in the
examples above, indicates that
the method should not return a
value.
public class Main {

static int myMethod(int x) {


• If you want the method to return 5 + x;
return a value, you can use a }
data type (such as int, char, etc.)
instead of void, and use the public static void main(String[] args) {
[Link](myMethod(3));
return keyword inside the }
method.
}
Example
a method to sum two numbers

public class Main {

static int myMethod(int x, int y) {


return x + y;
}

public static void main(String[] args) {


int z = myMethod(5, 3);
[Link](z);
}

}
Method scope
• Variables are only accessible inside the region they are created.
This is called scope.

• Variables declared directly inside a method are available


anywhere only in this method .
Java OOP
Object-oriented Programming
Java OOP
• OOP stands for Object-Oriented Programming.

• Procedural programming is about writing procedures or methods that


perform operations on the data, while object-oriented programming is
about creating objects that contain both data and methods.
Java OOP
• Object-oriented programming has several

advantages over procedural programming:

1- OOP is faster and easier to execute

2- OOP provides a clear structure for the programs

3- OOP makes the code easier to maintain, modify

and debug

4- OOP makes it possible to create full reusable

applications with less code and shorter development

time
Classes & objects

• Classes and objects are the


two main aspects of object-
oriented programming.
• A class is a template for
objects, and an object is an
instance of a class.
• When the individual objects
are created, they inherit all the
variables and methods from
the class.
Creating a class
• To create a class, use the keyword class

Creating an object
• An object is created from a class.
• To create an object, specify the class name, followed by the
object name,
and use the keyword new
Multiple objects
• You can create multiple objects of one class

• Multiple classes: You can also create an object of a class and


access it in another class.
Attributes
• Class attributes are variables within a
class.
• Accessing attributes You can access
attributes by creating an object of the
class, and by using the dot syntax
Methods
public class Main {

• A method is a block of code which only static void myMethod() {


runs when it is called to perform certain [Link]("welcome to Java");
actions. }

• You can pass data into a method , known public static void main(String[] args) {
as parameters, (inputs of a method) myMethod();
}
• You can also receive data from a method ,
known as return values, (outputs of a }
method)
• In OOP: Accessing (calling) methods
• You can access a method by creating an
object of the class, and by using the dot
syntax
Example 1

public class Main {

public void fullSpeed() {


[Link]("The car is going as fast as it
can!");
}

public void speed(int maxSpeed) {


[Link]("Max speed is: " + maxSpeed);
}

public static void main(String[] args) {


Main myCar = new Main(); // Create a myCar object
[Link](); // Call the fullSpeed() method
[Link](200); // Call the speed() method
}
}
Example 2 Calculate area and perimeter of rectangle

class Use_Rectangle {
public class Rectangle {

double length; public static void main(String args[])


double width; {
Rectangle rect = new Rectangle();
void Area()
{ [Link] = 15.5;
double area; [Link] = 22.3;
area = length * width;
[Link]("Area of rectangle is : " + area);
[Link]("Length = " +
}
[Link]);
void Perimeter() [Link]("Width = " +
{ [Link]);
double perimeter;
perimeter = 2 * (length + width); [Link]();
[Link]("Perimeter of rectangle is : " + perimeter); [Link]();
}
}
Example 2 Calculate area and perimeter of rectangle

• This example has 2 classes (Rectangle


and Use_Rectangle )
• Remember that the name of the java file
should match the class name.
So in this example, we have created two files
in the same directory:
• [Link]
• Use_Rectangle.java
Example 2 Calculate area and perimeter of rectangle
Thank you

You might also like