Lecture 3
Lecture 3
Object Oriented
Programming (OOP)
Instructor
2
Java Compilation Process
The Java compilation process is the series of steps that occur when you transform human-
readable Java source code into bytecode, which can be executed by the Java Virtual Machine
(JVM).
This process involves several stages:
Writing Java Source Code:
The first step is to write your Java source code in a plain text file. This code contains your program's
logic and is written in the Java programming language.
Compilation (javac):
The Java compiler, javac, is responsible for translating the Java source code into bytecode.
You run the javac command followed by the name of the Java source file (with the .java extension).
For example: javac MyProgram.java
If the code contains errors, the compiler will report them as compilation errors, and you'll need to fix
them before proceeding.
Bytecode Generation:
If there are no compilation errors, the Java compiler generates bytecode instructions.
These bytecode instructions are platform-independent and can be executed on any device that has a
Java Virtual Machine (JVM).
3
Java Compilation Process (Continued)
Class File Creation:
The compiler creates one or more binary files with a .class extension, where each .class file corresponds
to a class in your Java source code.
These .class files contain the bytecode for your classes and are used for execution.
Execution (java):
To run your Java program, you use the java command followed by the name of the class containing the
main method (the entry point of your program) but without the .class extension.
For example: java MyProgram
The JVM loads and executes the bytecode from the .class files.
It also performs Just-In-Time (JIT) compilation, which translates the bytecode into machine code for the
specific hardware and operating system.
Execution of the main Method:
The main method of the specified class is executed, starting the program's execution.
Termination:
Your program runs until it completes its tasks or encounters an error. At this point, it terminates, and the
JVM releases any allocated resources.
4
Java Compilation Process (Continued)
5
Structure of Java Program
Import Statements (if needed): At the
beginning of your Java program, you can include
import statements to bring in classes from other
packages or libraries that your program will use.
These statements are not always required.
Class Definition: The program begins with
the definition of a Java class. The class acts as a
blueprint for objects, defining their attributes and
behavior.
Instance Variables (Attributes): Within the
class, you can declare instance variables (also
called attributes) that represent the state of
objects created from the class.
Constructors: Constructors are special
methods with the same name as the class. They
initialize the objects created from the class. You
can have multiple constructors, including
overloaded constructors with different parameter
lists. Constructors are called when you create an
object using the new keyword.
6
Structure of Java Program
Methods (Functions): Inside
the class, you can define
methods that represent the
behavior or actions that objects
of this class can perform. These
methods can have parameters
and return values.
10
Classes and Objects
Object - Objects have states and behaviors.
Example: A dog has states - color, name, breed as
well as behaviors -wagging, barking, eating. An object
is an instance of a class.
Class - A class is a blueprint or template for creating
objects. It defines the structure and behavior of
objects of that class.
11
Class Example
public class Dog{
String breed;
int age;
String color;
void barking(){ }
void hungry(){ }
void sleeping(){ }
}
12
Constructor
Constructors have the same name as the class
No return type
Every class has a constructor.
If we do not explicitly write, Java compiler builds a
default constructor for that class.
Each time a new object is created, at least one
constructor will be invoked.
A class can have more than one constructor.
13
Constructor
public class Employee{
public Employee(){ }
public Employee(String name){
// This constructor has one parameter, name. }
}
14
Creating an Object
Declaration: A variable declaration with a variable
name with an object type.
Instantiation: The 'new' key word is used to create
the object.
Initialization: The 'new' keyword is followed by a
call to a constructor. This call initializes the new
object.
16
Creating an Object
public class Cat{
public Cat(String name){
// This constructor has one parameter, name.
System.out.println("Passed Name is :" + name );
}
}
17
Variable types
Local variables:
defined inside methods, constructors or blocks
destroyed when the method has completed.
Instance variables:
variables within a class but outside any method.
instantiated when the class is loaded.
can be accessed from inside any method, constructor or blocks of that particular
class.
Class variables:
declared with in a class, outside any method, with the static keyword.
Static variables can be accessed by calling with the class name .
ClassName.VariableName.
18
Local Variables
public class Test{
public void pupAge(){
int age = 0;
age = age + 7;
System.out.println("Puppy age is : " + age);
}
public static void main(String args[]){
Test test = new Test();
test.pupAge();
}
}
19
Instance Variables
import java.io.*;
public class Employee{// this instance variable is visible for any child class.
public String name; // salary variable is visible in Employee class only.
private double salary; // The name variable is assigned in the constructor.
public Employee (String empName){
name = empName;
}
public void setSalary(double empSal){// The salary variable is assigned a value.
salary = empSal;
}
public void printEmp(){// This method prints the employee details.
System.out.println("name : " + name );
System.out.println("salary :" + salary);
}
public static void main(String args[]){
Employee empOne = new Employee(“Ali"); empOne.setSalary(1000); empOne.printEmp();
}
} 20
Class/Static Variable
import java.io.*;
public class Employee{ // salary variable is a private static variable
private static double salary;
// DEPARTMENT is a constant
public static String DEPARTMENT = "Development“ ;
public static void main(String args[]){
salary = 1000;
System.out.println(Employee.DEPARTMENT+"average salary:"+
Employee.salary);
}
21
Java Modifier Type
Access Control Modifiers:
Access control modifiers determine the visibility and accessibility of classes, methods, and fields
from different parts of your Java program. There are four main access control modifiers in Java:
public: Members with this modifier are accessible from anywhere, both within the same package and
from outside the package.
protected: Members with this modifier are accessible within the same package and by subclasses,
whether they are in the same package or not.
private: Members with this modifier are accessible only within the same class. They are not visible from
outside the class.
default (package-private): If no access modifier is specified (i.e., no modifier is used), the member is
considered "package-private." It is accessible only within the same package.
24