Cse205 1
Cse205 1
OOP- Java
12/01/2025
What is?
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
Object-oriented programming aims to implement real-world
entities like inheritance, abstraction, polymorphism, etc. in
programming. The main aim of OOPs is to bind together the data
and the functions that operate on them so that no other part of the
code can access this data except that function.
Object
Any entity that has state and behavior is known as an object. For
example, a chair, pen, table, keyboard, bike, etc. It can be physical or
logical.
Class
Collection of objects is called class. It is a logical entity.
Inheritance
When one object acquires all the properties and behaviors of a parent
object, it is known as inheritance. It provides code reusability.
Polymorphism
If one task is performed in different ways, it is known as polymorphism.
For example: to convince the customer differently, to draw something,
for example, shape, triangle, rectangle, etc.
What is Java?
Java is a popular programming language, created in 1995.
It is used for:
Main.java
System.out.println("Hello World");
}
Java Syntax
Main.java
System.out.println("Hello World");
}
Explanation: Code Breakdown
public class Main {
System.out.println("Hello World");
1. The Java runtime looks for the main method in the Main class.
2. The main method executes the statement inside it:
System.out.println("Hello World");.
3. The text "Hello World" is printed to the console.
Output
Hello World
You can add as many println() methods as you want. Note that it will add a
new line for each method:
Example
System.out.println("Hello World!");
System.out.println("It is awesome!");
Double Quotes
Text must be wrapped inside double quotations marks "".
Example
System.out.println("This sentence will work!");
The only difference is that it does not insert a new line at the end of the
output:
Example
System.out.print("Hello World! ");
Example
System.out.println(3);
System.out.println(358);
System.out.println(50000);
You can also perform mathematical calculations inside the println() method:
Example
System.out.println(3 + 3);
Example
System.out.println(2 * 5);
Java Comments
Comments can be used to explain Java code, and to make it more readable.
It can also be used to prevent execution when testing alternative code.
Single-line Comments
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by Java (will not be
executed).
Example
// This is a comment
System.out.println("Hello World");
Example
System.out.println("Hello World"); // This is a comment
Java Multi-line Comments
Multi-line comments start with /* and ends with */.
Example
/* The code below will print the words Hello World
System.out.println("Hello World");
Syntax
type variableName = value;
Example
Create a variable called name of type String and assign it the value "John".
Then we use println() to print the name variable:
System.out.println(name);
Example
Create a variable called myNum of type int and assign it the value 15:
System.out.println(myNum);
You can also declare a variable without assigning the value, and assign the
value later:
Example
int myNum;
myNum = 15;
System.out.println(myNum);
Note that if you assign a new value to an existing variable, it will overwrite
the previous value:
Example
Change the value of myNum from 15 to 20:
System.out.println(myNum);
Final Variables
If you don't want others (or yourself) to overwrite existing values, use
the final keyword (this will declare the variable as "final" or "constant",
which means unchangeable and read-only):
Example
final int myNum = 15;
Example
int myNum = 5;
Example
String name = "John";
You can also use the + character to add a variable to another variable:
Example
String firstName = "John ";
System.out.println(fullName);
For numeric values, the + character works as a mathematical operator (notice
that we use int (integer) variables here):
Example
int x = 5;
int y = 6;
Example
Instead of writing:
int x = 5;
int y = 6;
int z = 50;
System.out.println(x + y + z);
int x = 5, y = 6, z = 50;
System.out.println(x + y + z);
One Value to Multiple Variables
You can also assign the same value to multiple variables in one line:
Example
int x, y, z;
x = y = z = 50;
System.out.println(x + y + z);
Java Identifiers
All Java variables must be identified with unique names.
Example
// Good
int m = 60;
Example
// Student data
// Print variables
Example
// Create integer variables
int length = 4;
int width = 6;
int area;
// Print variables
You will learn more about data types in the next class.