n n public interface My Interface public abstract
קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד n שירותים במנשק הם תמיד מופשטים וציבוריים n public interface My. Interface { public abstract int foo 1(int i); int foo 2(int i); } The modifiers of foo 1 and foo 2 are the same. 3
מנשקים public interface Foo { public void bar() throws Exception; } public class Foo. Impl implements Foo { public void bar() { System. out. println("No exception is thrown"); } public static void main(String args[]) { Foo foo = new Foo. Impl(); foo. bar(); ? למה , האם הקוד מתקמפל? אם לא } ? למה , האם יש שגיאת ריצה? אם יש , אם כן } ? מה הפלט , אחרת : שגיאת קומפילציה "Unhandled exception type Exception" 4
המשך - מנשקים public interface Foo { public void bar() throws Exception; } public class Foo. Impl implements Foo { public void bar() { System. out. println("No exception is thrown"); } public static void main(String args[]) { Foo. Impl foo = new Foo. Impl(); foo. bar(); ? למה , האם הקוד מתקמפל? אם לא } ? למה , האם יש שגיאת ריצה? אם יש , אם כן } ? מה הפלט , אחרת : פלט "No exception is thrown" 5
מנשקים וירושה Consider the following class hierarchy: Interface Animal {…} class Dog implements Animal{…} class Poodle extends Dog {…} class Labrador extends Dog {…} Animal Dog Poodle Labrador Which of the following lines (if any) will not compile? poodle = (Poodle) dog; Poodle poodle = new Poodle(); Animal animal = (Animal) poodle; -No compilation error -Runtime Exception Dog dog = new Labrador(); - Compilation Error animal = dog; Type mismatch: cannot convert poodle = dog; from Dog to Poodle Labrador labrador = (Labrador) dog; -No compilation error -No Runtime Exception 6
מנשקים וירושה class A { public void print() { System. out. println("A"); } } ? האם יש שגיאה class B extends A implements C { } interface C { void print(); } אין שגיאות קומפילציה כברירת מחדל public 7
מנשקים וירושה class A { void print() { System. out. println("A"); } } ? האם יש שגיאה class B extends A implements C { } interface C { void print(); } : שגיאת קומפילציה The inherited package method A. print() cannot hide the public abstract method in C 8
The following table shows the access to members permitted by each modifier 9
דריסה של שירותים public class A { public void print() { System. out. println("A"); } } public class B extends A { public void print() { System. out. println("B"); } } : הפלט B B public class C { public static void main(String[] args){ B b = new B(); A a = b; b. print(); a. print(); - אין צורך ב casting } } ? למה , האם הקוד מתקמפל? אם לא ? למה , האם יש שגיאת ריצה? אם יש , אם כן 10 ? מה הפלט , אחרת
דריסה של שירותים וניראות public class A { public class C { public void print() { public static void main(String[] System. out. println("A"); args) { } B b = new B(); } b. print(); } public class B extends A { } protected void print() { System. out. println("B"); } } : שגיאת קומפילציה "Cannot reduce the visibility of the inherited method from A" ? למה , האם הקוד מתקמפל? אם לא ? למה , האם יש שגיאת ריצה? אם יש , אם כן 11 ? מה הפלט , אחרת
(2) דריסה של שירותים וניראות public class A { public class C { protected void print() { public static void main(String[] System. out. println("A"); args) { } B b = new B(); } b. print(); } public class B extends A { } public void print() { System. out. println("B"); } } : הפלט B ? למה , האם הקוד מתקמפל? אם לא ? למה , האם יש שגיאת ריצה? אם יש , אם כן ? מה הפלט , אחרת 12
הורשה public class B extends A { public class A { public void foo() { System. out. println("A. foo()"); System. out. println("B. foo()"); } } public void bar() { System. out. println("A. bar()"); foo(); } public static void main(String[] args) { A a = new B(); a. bar(); } } } : הפלט A. bar() B. foo() ? למה , האם הקוד מתקמפל? אם לא ? למה , האם יש שגיאת ריצה? אם יש , אם כן ? מה הפלט , אחרת 13
(2) הורשה public class B extends A { public class A { private void foo() { public void foo() { System. out. println("A. foo()"); System. out. println("B. foo()"); } } public void bar() { System. out. println("A. bar()"); foo(); } public static void main(String[] args) { A a = new B(); a. bar(); } } } : הפלט A. bar() A. foo() ? למה , האם הקוד מתקמפל? אם לא ? למה , האם יש שגיאת ריצה? אם יש , אם כן ? מה הפלט , אחרת 14
הורשה ובנאים public class C { public static void main(String[] args) { A a = new B(); A() { foo(); } String s = "a. bar = " + a. bar System. out. println(s); public void foo() { a. foo(); System. out. println("A. foo(): bar = " + bar); } } public class A { String bar = "A. bar"; public class B extends A { String bar = "B. bar"; B() { foo(); } public void foo() { System. out. println("B. foo(): bar = " + bar); } What is the output? : הפלט B. foo(): bar = null B. foo(): bar = B. bar a. bar = A. bar B. foo(): bar = B. bar } 15
(2) הורשה ובנאים public class A { protected B b = new B(); public A() { System. out. println("in A: no args. "); } public A(String s) { System. out. println("in A: s = " + s); } } public class B { public B() { System. out. println("in B: no args. "); } } public class C extends A { protected B b; public C() { System. out. println("in C: no args. "); } public C(String s) { System. out. println("in C: s = " + s); } } public class D { public static void main(String args[]) { C c = new C(); A a = new C(); } } What is the output? : הפלט in in in B: A: C: no no no args. 16
(3) הורשה ובנאים public class A { protected B b = new B(); public A() { System. out. println("in A: no args. "); } public A(String s) { System. out. println("in A: s = " + s); } } public class B { public B() { System. out. println("in B: no args. "); } } public class C extends A { protected B b; public C() { System. out. println("in C: no args. "); } public C(String s) { System. out. println("in C: s = " + s); } } public class D { public static void main(String args[]) { C c = new C("c"); A a = new C(“a"); } } What is the output? : הפלט in B: no in A: no in C: s args. = c args. = a 17
דריסה והעמסה של שירותים public class A { public float foo(float a, float b) throws IOException { } } public class B extends A { … } ? B - אילו מהשירותים הבאים ניתן להגדיר ב 1. 2. 3. 4. float foo(float a, float b){…} public int foo(int a, int b) throws Exception{…} public float foo(float a, float b) throws Exception{…} public float foo(float p, float q) {…} 18
הורשה ודריסת שירותים public class A { public void foo() {…} } public class B extends A { public void foo() {…} } של foo- האם אפשר לקרוא ל ? B מתוך A : תשובה super. foo() דרך 19
(2) הורשה ודריסת שירותים public class A { public void foo() {…} } public class B extends A { public void foo() {…} } של foo- האם אפשר לקרוא ל ? C מתוך A : תשובה , אי אפשר super. foo() לא חוקי - public class C extends B { public void foo() {…} } 20
מחלקות פנימיות public class Test { public int a = 0; private int b = 1; נגישים מהשורה a-e - אילו משתנים מ ? המסומנת public void foo(final int c) { int d = 2; d- כולם חוץ מ : תשובה class Inner. Test { private void bar(int e) { } } 21
סיכום - מחלקות פנימיות Type Scope Inner Define an interface inside Fields access Static nested member no yes Only static Inner nonstatic member yes no Static and non-static local Local scope yes no final local variables or parameters that are accessible in the scope of the block anonymous Only the point where it is defined yes no final local variables or parameters that are accessible in the scope of the block 23
enum All enums implicitly extend java. lang. Enum An enum cannot extend anything else. The constructor for an enum type must be package-private or private access. You cannot invoke an enum constructor yourself. fixed set of constants 24
enum Output: Mondays are bad. SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY static values method that returns an array containing all of the values of the enum in the order they are declared 25
אוספים גנריים public static void func(Hash. Set<String> set){ for (String s : set){ System. out. println(s); } } public static void main(String[] args){ Hash. Set<String> my. Set = new Hash. Set<String>(); my. Set. add("abc"); my. Set. add("dce"); func(my. Set); } : ניתן )ואפילו רצוי( לכתוב גם new Hash. Set<>(); 26
אוספים גנריים public static void func(Hash. Set<String> set){ for (String s : set){ System. out. println(s); } } public static void main(String[] args){ Hash. Set<String> my. Set = new Hash. Set<String>(); my. Set. add("abc"); my. Set. add("dce"); func(my. Set); } ? בד"כ נשתמש Hash. Set מדוע הפונקציה דורשת לקבל ? ע"מ Set האם נוכל לשלוח לפה כל. בטיפוס כמה שיותר כללי נוכל לשלוח אפילו , למען האמת. אין סיבה שלא , המימוש שלה . Collection 28
אוספים גנריים public static void func(Collection<? extends Rectangle> set){ for (Rectangle r : set){ System. out. println(r. get. Area()); } } public static void main(String[] args){ Set<Rectangle> my. Set = new Hash. Set<>(); my. Set. add(new Rectangle(5, 6)); my. Set. add(new Rectangle(1, 2)); func(my. Set); } 31
- Slides: 31