Professional Documents
Culture Documents
Oop Sum
Oop Sum
Oop Sum
Class
ﯾﺗﻛون ﻣن ﺟزﺋﯾن ﻣﮭﻣﯾن ھﻣﺎ اﻟداﺗﺎ و اﻟﻣﯾﺛود
1)State: data fields with their current values (attributes).
1) Behavior: methods of an object (actions).
UML
# protected
_ private
+ Public
Ahmed Static
Constructor
Can’t Use void with constructor ??
} اﻟﻛوﻧﺳﺗراﻛور ﻣﯾﻧﻔﻌش ﯾﻛون ﻟﮫ
Return type
Void --> return nothing ;}
If a class has no constructor, Java gives it a default constructor with no
parameters that sets all fields to default values
Number zero
Bool false
String null
ﺑﯾﻛون ﻓﯾﮫ ﻛوﻧﺳﺗراﻛﺗور ﻣﺑدﺋﻲ ﻟو اﻧﺎ ﻣﻌﻣﻠﺗش اي واﺣد ﻓﻲ اﻟﻛﻼس ﺑﯾﺗﻌﻣل ﺗﻠﻘﺎﺋﻲ ﻣن
Compiler
Note
private n;
Public Book(String n){
n=n; // there are 2 value with same name // you can use this
this.n=n; // correct }
Constructor Call Constructor With THIS
Static
ﺑﺗﮭﺗم ﺑﺎﻟﻛﻼس ﺑﺎﻟﻛﺎﻣل وﻟﯾس اوﺑﺟﻛت ﻣﻧﮫ ﺑﻣﻌﻧﻲ ان أي ﻣﯾﺛود ﻣن اﻟﻧوع ده او داﺗﺎ ﺑﯾﺗﻧﺎدي
ﻋﻠﯾﮭﺎ ﺑﺎﻟﺷﻛل ده
Classname.StaticMehod
ﻣﺛﺎل وﻧﻘطﮫ ﻣﮭﻣﮫ :ﻟو اﻧﺎ ﻋﻣﻠت ﻓﻲ اﻟﻛوﻧﺳﺗراﻛﺗور اﻧﮫ ﻛل ﻣﺎ ﯾﻌﻣل اوﺑﺟﯾﻛت ﺟدﯾد ھﺣط
ﻛﺎوﻧﺗر ﯾزﯾد
;)(Book b1 = new Book
;)(Book b2= new Book
;)(Book b3 = new Book
اﻟﻣﻔروض ﻛده اﻟﻛﺎوﻧﺗر ﺑﺗﺎﻋﻲ ھﯾﺑﻘﻲ ب 3ﻋﺷﺎن ﻋﻣﻠت 3اوﺑﺟﯾﻛت واﻟﻛﺎوﻧﺗر اﻧﺎ ﻋﺎﻣﻠﮫ
Static
; B1.count; Book.countاﻟﻘﯾﻣﺗﯾن ھﯾطﺑﻌوا 3
Important
If The method is static then Any variable in the method must be static
Else won’t be compile
If the method without static then can access Static Variable without any
errors
Static variables and methods can be used from instance or static
methods in the class.
Instance variables and methods can only be used from instance
methods, (i.e., static
Example
Wrong code
Example :
F Static
f instance
Visibility Modifiers
Public Can access in any another package or class
Private in only same class
Default ﻣش ﺑﺣط ﺟﻧب اﻟﻔﺎﻟﯾو أي ﺣﺎﺟﮫ in the same package
Protected in the same package
Accessing private state •
Client code won't compile if it accesses private fields:
Visibility modifiers are used for the members of the class, not local
variables inside the methods.
• Using a visibility modifier on local variables would cause a
compilation error.
• In most cases, the constructor should be public. – However, if you
want to prohibit the user from creating an instance of a class, you can
use a private constructor
Important
You must create object of all Array
Ex:
Inheritance
CHECK THIS (: //
B's constructor is invoked
A's constructor is invoked
This is because when a subclass constructor is invoked, it implicitly calls the superclass
constructor first, unless the subclass constructor explicitly calls another constructor of
the superclass using the super keyword. In this case, the A class constructor does not
use the super keyword, so it calls the default constructor of the B class, which prints
“B’s constructor is invoked”. Then, the A class constructor prints “A’s constructor is
invoked”.
Important Ex
ﺑﺎﺧﺗﺻﺎر ﺑﺳﺗﺧدم اﻻﺑﺳﺗراﻛت ﻟﻣﺎ ﯾﻛون ﻋﻧدي ﺣﺎﺟﮫ ﻋﺎﻣﮫ وھﺑدا اﺧﺻﺻﮭﺎ ﻟﻛل
ﺳب ﻛﻼس
ﻟﻣﺎ اﻋﻣل اﻧﮭﯾرت ﻣﻧﮭم ھﻛون ﻣﺿطر اﻧﻲ اﻋﻣل ﻧﻔس اﻟﻣﯾﺛود ﺑﻧﻔس اﻟﺑراﻣﯾﺗر وﻟﻛن ﻣﻣﻛن ﻛل واﺣد ﯾﻛون
ﻟﯾﮭﺎ اﻣر ﻣﺧﺗﻠف ﻋن اﻟﺗﺎﻧﯾﮫ
ﻣﺛﺎل اﺧر
اﻟﺷﻛل دي ﺣﺎﺟﮫ ﻋﺎﻣﮫ وﻟﯾﮭم ﻣﺳﺎﺣﮫ وﻣﺣﯾط ﺑس اﻟﻣﺳﺗطﯾل ﻏﯾر اﻟﻣرﺑﻊ وھﻛذا
Notes
ﻣﯾﻧﻔﻌش ﺗﺎﺧد اوﺑﺟﻛت ﻣن اﺑﺳﺗراﻛت ﻛﻼس •
ﻟو ﻋﻣﻠت اﻧﮭﯾرت ﻟﻛﻼس ﺟواه اﺑﺳﺗراﻛت ﻣﯾﺛود ﻻزم ﺗﻛﺗب ﻧﻔس اﻟﻣﯾﺛود ﺑﻧﻔس اﻟﺷﻛل ﻋﺷﺎن ﻣﯾﻛون ﻓﯾﮫ
اﯾرور ﻓﻲ اﻟﻛﻼس
ﻟو اﻟﻛﻼس ﻧﻔﺳﮫ اﺑﺳﺗراﻛت واﻟﻣﯾﺛود ﻻ ﻋﺎدي ﻣﻣﻛن ﻣﻌﻣﻠش اوﻓررﯾد ﻟﯾﮭﺎ NOTE//
اﻧﻣﺎ ﻟو اﻟﻣﯾﺛود ذات ﻧﻔﺳﮭﺎ اﺑﺳﺗراﻛت ﻻزم ﯾﺗﻌﻣل ﻧﻔس اﻟﻣﯾﺛوذ واﻻ ھﯾﺟﯾب اﯾرور
Final Var , Final Method
A final variable can be explicitly initialized only once.
A reference variable declared final can never be reassigned to
refer to a different object.
With variables, the final modifier often is used with static to
make the constant a class variable.
Final Variable… EX
: public class Test { final int value = 10;
public void changeValue() {
value = 12; // will give an error }
Final Method
Final Class
final class is a class that cannot be extended by any
other class
EX : FROM GEEK
class Base {
final public void show() {
System.out.println("Base::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
ANS // Error
Interface
An interface in Java is a blueprint of a class.
It has static constants and abstract methods.
There can be only abstract methods in the Java interface (no complete
methods inside!)
It is used to achieve abstraction and multiple inheritance in Java
Interface fields are public, static and final by default, and the
methods are public and abstract.
Example 1:
interface Drawable{ void draw(); }
INSTANCE OF OPERATOR
ﻟﮫ ﻟﻣﺎ ﺑﻌﻣل ﺳوﺑر ﻛﻼس وﯾﺑﻘﻲ ﻓﯾﮫ ﻛذا ﻛﻼس ﺑﯾﻌﻣﻠوا اﻧﮭﯾرت
واطﺑﻖ ﻓﻛره اﻟﺑوﻟﯾﻣورﻓﯾﻣز ﻋﻠﯾﮭم اﻧﻲ اﻋﻣل اري ﻟﯾﺳت ﻣن اﻟﺳوﺑر ﻛﻼس وادﺧل ﻓﯾﮭﺎ ﻛذا اوﺑﺟﻛت ﻣن
ﻛﻼﺳﯾز ﻣﺧﺗﻠﻔﮫ ﺑﺗﻌﻣل اﻧﮭرت ﻣﻧﮫ
ﻣﺛﻼ ﻟو داﯾره ﻓﻠﯾﮭﺎ ﻣﯾﺛود ﻣﻌﯾﻧﮫ ﻟطﺑﺎﻋﮫ اﻟﻣﺳﺎﺣﮫ وﻟو ﻣﺳﺗطﯾل وھﻛذا
ﻟﻣﺎ ﺑﺎﺟل اﻋﻣل ﻟوب ﻋﻠﻲ اﻻرﯾﻠﯾﺳت ﺑﺷوف ﻛل اوﺑﺟﯾﻛت ﺟواھﺎ ھو ﻣن اﻧﮭﻲ ﻛﻼس
if object from Arraylist instanceof circle
ﯾﺑﻘﻲ اطﺑﻊ اﻻوﺑﺟﯾﻛت ﺑﺗﺎع اﻟداﯾره
circle.getArea
ھﯾﺑﻘﻲ ده اوﺑﯾﺟﻛت ﻣن اﻟﻣﺳﺗطﯾل واطﺑﻊ ﻣﺳﺎﺣﮫ اﻟﻣﺳﺗطﯾل else
Excep�on Handling
Ex :
}
}
// ﻣﮭم ﺟداااااااااااااااااااااااااااااااااا
class Main {
public static void main(String[] args) {
System.out.println(1.0/0);
}]
ANS //
Infinity
This is because dividing a floating-point value by zero does not throw an exception in
Java, but instead produces a special value called Infinity. This value is defined in the
Double and Float classes as a constant.
You can make new excep�on if user put nega�ve
ﻟو دﺧل رﻗم ﺳﺎﻟب ﻣش ھﯾﺑﻘﻲ اﻛﺳﺑﺷن ﺑس اﻟﻌﻣر ﻣش ﺑﺎﻟﺳﺎﻟب
(:
break;
} catch (InputMismatchException e) {
System.out.println(e);
} catch (ArrayIndexOutOfBoundsException s) {
System.out.println(s);
} catch (ArithmeticException v) {
System.out.println(v);
}
}
Note
اﻟﺑرﻧﺎﻣﺞ ھﯾﻘف ﻣﻊ اول اﻛﺳﺑﺷن ﻟو ﻣﻔﯾش ﻟوب وھﯾطﺑﻊ اﻷول ﺑس وﻣش ھﯾﻛﻣل
Finally Block
Answers :
inside proc
AprocA'sfinally
Exception caught
inside procB
procB'sfinally
inside procC
procC'sfinally
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
Option A, B, and C are incorrect based on the program logic described above.
Remember that either a catch or a finally statement must follow a try. Since the finally is
present, the catch is not required.
System.out.print("Start ");
try
{
System.out.print("Hello world");
throw new FileNotFoundException();
}
System.out.print(" Catch Here "); /* Line 7 */
catch(EOFException e)
{
System.out.print("End of file exception");
}
catch(FileNotFoundException e)
{
System.out.print("File not found");
}
Line 7 will cause a compiler error. The only legal statements after try blocks are
either catch or finally statements.
Option B, C, and D are incorrect based on the program logic described above. If line 7
was removed, the code would compile and the correct answer would be Option B.