JavaAdel Al Jaffan
Enas AL Saeek
Reference :
• 1- Thinking in java
• 2- OOP in java (Arabic)
• 3- White Hat lectures (2013-2014)
• 4- Online tutorials ( YouTube – Coursera )
• 5- MORE …. !!!
Object Oriented Programming (O O P)
Object Oriented Programming (O O P)
Let us code !!!
• 1st :
install Java Development Kit “JDK”
• 2nd :
what about IDE : “Netbeans” or “Eclipse” ?
• 3rd :
We must know how to create our first java project ! :D
Classes (only)
• How can I make my
FIRST “CLASS”
Classes (only)
Printing “any thing” !
hi
2
Variables
int a , b, c; // Declares three integers, a, b, and c.
int a = 10, b = 10 ; // Example of initialization
byte B = 22; // initializes a byte type variable B.
double pi = 3.14159; // declares and assigns a value of PI .
char a = 'a'; // the char variable a is initialized with value 'a‘
Variables (casting)
If / else
If / else
Switch case
switch (int value) {
case 1: //
break;
case 2 : //
break;
default : //
break ;
}
Switch case
For loop
While loop
Declaring method ( function/procedure )
• Function :
return type method name ( type variable1, type variable 2 , ..… )
{
// code block
return variable ” same return type”
}
Declaring method ( function/procedure )
• procedure :
void method name ( type variable1, type variable 2 , ..… )
{
// code block
}
Example 1 :
• Make 3 method (add , sub , mult )
Let’s code
Constructors
• 3 TYPES !!!
1) default constructor
We do not write it !!!
2) with non-arguments
s3) with arguments
Objects !!!
• Class_name object_name ; // without initialize … runtime error !
• Class_name object_name = new Class_name ( ) ;
• Class_name object_name = new Class_name ( value ) ;
Example 1 :
Let us code !!!
Example 1 :
Example 1 :
Output :
mewoooo
7
8
Inheritance in java -1
Inheritance in java -2
Inheritance in java -3
Output :
i'm the father
i'm the son
Inheritance in java -4
Overload in java
The conditions for method overloading :
1.) The number of parameters is different for the methods.
2.) The parameter types are different ,
(like changing a parameter that was a float to an int).
Overload in java
How to NOT overload methods :
1. Just changing the return type of the method. If the return type
of the method is the only thing changed, then this will result in
a compiler error.
1. Changing just the name of the method parameters, but not
changing the parameter types. If the name of the method
parameter is the only thing changed then this will also result in
a compiler error.
Overload in java
Example 1 :
Overload in java
Example 2 :
Overload in java
Example 3 :
Overload in java
Example 4 :
Inheritance in java - Override
Example !!!!
Inheritance in java - Override
Output ?!!!!
Exception handling
Try {
//code block
} catch ( Exception e ) {
// code block
} finally {
// code block
}
Example 1 :
- Throw Exception after division operation
Let us code !!!
Good Bye
Any questions ?