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

Lecture 2 Structure, Methods

The document discusses key concepts in object-oriented programming and Java including: 1) The structure of a Java program includes classes with a main method that can print output. Java code is compiled into bytecode that runs on the Java Virtual Machine (JVM). 2) Classes define the template for objects with attributes and methods. Objects are instances of classes that occupy memory at runtime. 3) Methods perform specific tasks and are declared with a return type, name, parameters, and body. Methods can be called to reuse code. Actual parameters passed during a method call are assigned to the formal parameters declared in the method signature.

Uploaded by

fatimamubarak567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lecture 2 Structure, Methods

The document discusses key concepts in object-oriented programming and Java including: 1) The structure of a Java program includes classes with a main method that can print output. Java code is compiled into bytecode that runs on the Java Virtual Machine (JVM). 2) Classes define the template for objects with attributes and methods. Objects are instances of classes that occupy memory at runtime. 3) Methods perform specific tasks and are declared with a return type, name, parameters, and body. Methods can be called to reuse code. Actual parameters passed during a method call are assigned to the formal parameters declared in the method signature.

Uploaded by

fatimamubarak567
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Object Oriented

Programming
Lecture 2

BSCS 2(Afternoon)
Spring 2024
[email protected]
Topics to be covered according to
outline

• Structure of Java
program,
• Java Compilation
Process
• Compiling and running
a Java program
• Methods,
• Introducing methods,

• Method signatures,
Arguments, and
parameters
Structure of Java Code

public class MyFirstJavaProgram {


/* This is my first java program. This will print
'Hello World' as the output */
public static void main(String []args) {
System.out.println("Hello World");
// prints Hello World
}
}
java compilation process diagram
Java Virtual Machine
• Java was designed with a concept of

‘write once and run


everywhere’
• The JVM is the environment in which Java programs execute. It is a
software that is implemented on top of real hardware and operating
system.
• When the source code (.java files) is compiled, it is translated into byte
codes and then placed into (.class) files.
• The JVM executes these bytecodes.
• A JVM can either interpret the bytecode one instruction at a time
• The JVM must be implemented on a particular platform before compiled
programs can run on that platform.
Integrated development environment

• An integrated or interactive development environment (IDE)


• is a software application
• provides comprehensive facilities to computer programmers for software
development.
• normally consists of a source code editor, build automation tools and a
debugger
• Some IDEs contain a compiler, interpreter, or both, such as Net Beans
and Eclipse
• Some IDEs support multiple languages
• Many modern IDEs also have a class browser, an object browser, and a
class hierarchy diagram
OOP Concepts

• Classes
• Objects
• Instance
• Method
• Message Parsing
Classes and Objects
Classes and objects
Classes and Objects
Class

• Class is a collection of objects


• Class is not a real world entity
• It is a template or blue print
• Class does not occupy memory
Syntax of Class in Java
Objects

• Object is an instance of class


• Object is a real world entity
• Object occupies memory
• Object consists of:
• Identity
• Attributes
• behavious
Attributes

• An attribute is a named object with associated values.


• Each value in the attribute corresponds to a Java object of some
type.
• The variables created within the Java classes are referred as
class attributes or fields
Methods

• A block of code that, when called, performs specific actions


mentioned in it
• Advantages of method:
• Code reusability
• Code optimisation
Classes, Objects, Attributes and Methods
Java Methods

• A method is a block of code that performs a specific task.


• Suppose you need to create a program to create a circle and color
it. You can create two methods to solve this problem:
• a method to draw the circle
• a method to color the circle
• Dividing a complex problem into smaller chunks makes your
program easy to understand and reusable.
Java Methods
• In Java, there are two types of methods:
• User-defined Methods: We can create our own method based on
our requirements.
• Standard Library Methods: These are built-in methods in Java
that are available to use
Syntax of Method in java
Declaring a Java Method

•returnType - It specifies what type of value a method returns For


example if a method has an int return type then it returns an integer
value.

If the method does not return a value, its return type is void.

•methodName - It is an identifier ier that is used to refer to the


particular method in a program.

•method body - It includes the programming statements that are used


to perform some tasks. The method body is enclosed inside the curly
braces { }.
modifier static returnType nameOfMethod (parameter1, parameter2,
...) {
// method body
}

• modifier - It defines access types whether the method is public,


private, and so on. To learn more, visit Java Access Specifier.
• static - If we use the static keyword, it can be accessed without
creating objects
Calling a Method in Java
Calling a Method in Java
Method Signature
Method Name

• Methods should be verbs, in mixed case


• with the first letter lowercase
• with the first letter of each internal word capitalized.
Methods Arguments and Parameters
Actual Parameters And Formal Parameters
Actual Parameters And Formal Parameters
Local and Global variables
Global variables
Local and Global variables
What is Happening here?
We will discuss it in detail while discussing properties of
objects
We will know it better after
understanding object’s behavior
Questions Are Welcome

You might also like