Class Hierarchy and Interface Implementation Example

slide1 n.w
1 / 32
Embed
Share

"Explore a class hierarchy involving animals, inheritance, interface implementation, and method overriding with potential compilation errors and exceptions. Understand how the relationships between classes and interfaces impact code compilation and execution."

  • Class Hierarchy
  • Interface Implementation
  • Compilation Errors
  • Exceptions
  • Inheritance

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 1 12 1

  2. ! : 2

  3. , public interface MyInterface { public abstract int foo1(int i); int foo2(int i); } The modifiers of foo1 and foo2 are the same. 3

  4. public interface Foo { public void bar() throws Exception; } public class FooImpl implements Foo { public void bar() { System.out.println("No exception is thrown"); } } public static void main(String args[]) { Foo foo = new FooImpl(); foo.bar(); } ? ? ? ? , ? , , , : 4 "Unhandled exception type Exception"

  5. - public interface Foo { public void bar() throws Exception; } public class FooImpl implements Foo { public void bar() { System.out.println("No exception is thrown"); } } public static void main(String args[]) { FooImpl foo = new FooImpl(); foo.bar(); } ? ? ? ? , ? , , , : 5 "No exception is thrown"

  6. Consider the following class hierarchy: Animal Interface Animal { } class Dog implements Animal{ } class Poodle extends Dog { } class Labrador extends Dog { } Dog Poodle Labrador Which of the following lines (if any) will not compile? poodle = (Poodle) dog; -No compilation error -Runtime Exception Poodle poodle = new Poodle(); Animal animal = (Animal) poodle; Dog dog = new Labrador(); animal = dog; poodle = dog; - Compilation Error Type mismatch: cannot convert from Dog to Poodle Labrador labrador = (Labrador) dog; -No compilation error -No Runtime Exception 6

  7. class A { publicvoid print() { System.out.println("A"); } } class B extends A implements C { } ? interface C { void print(); } public 7

  8. class A { System.out.println("A"); } } class B extends A implements C { } void print() { ? : interface C { void print(); } The inherited package method A.print() cannot hide the public abstract method in C 8

  9. The following table shows the access to members permitted by each modifier 9

  10. publicclass A { publicvoid print() { System.out.println("A"); } } publicclass B extends A { publicvoid print() { System.out.println("B"); } } publicclass C { publicstaticvoid main(String[] args){ B b = new B(); A a = b; - casting b.print(); a.print(); } } : ? ? ? ? , B B ? , , , 10

  11. publicclass A { publicvoid print() { System.out.println("A"); } } publicclass C { publicstaticvoid main(String[] args) { B b = new B(); b.print(); } publicclass B extends A { protectedvoid print() { System.out.println("B"); } } } : ? ? ? ? , "Cannot reduce the visibility of the inherited method from A" ? , , , 11

  12. ( 2 ) publicclass A { protected void print() { System.out.println("A"); } } publicclass C { publicstaticvoid main(String[] args) { B b = new B(); b.print(); } publicclass B extends A { public void print() { System.out.println("B"); } } } ? ? ? ? , : ? , , , B 12

  13. publicclass B extends A { publicvoid foo() { System.out.println("B.foo()"); } publicclass A { publicvoid foo() { System.out.println("A.foo()"); } publicvoid bar() { System.out.println("A.bar()"); foo(); } } publicstaticvoidmain(String[] args) { A a = new B(); a.bar(); } } : ? ? ? ? , A.bar() B.foo() ? , , , 13

  14. ( 2 ) publicclass B extends A { publicvoid foo() { System.out.println("B.foo()"); } publicclass A { privatevoid foo() { System.out.println("A.foo()"); } publicvoid bar() { System.out.println("A.bar()"); foo(); } } publicstaticvoidmain(String[] args) { A a = new B(); a.bar(); } } ? ? ? ? , : ? , , , A.bar() A.foo() 14

  15. publicclass A { String bar = "A.bar"; publicclass C { publicstaticvoid main(String[] args) { A a = new B(); System.out.println("a.bar = " + a.bar); a.foo(); } } A() { foo(); } publicvoid foo() { System.out.println("A.foo(): bar = " + bar); } } publicclass B extends A { String bar = "B.bar"; B() { foo(); } What is the output? B.foo(): bar = null B.foo(): bar = B.bar a.bar = A.bar B.foo(): bar = B.bar : publicvoid foo() { System.out.println("B.foo(): bar = " + bar); } } 15

  16. ( 2 ) publicclass 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); } } publicclass B { public B() { System.out.println("in B: no args."); } } What is the output? : in B: no args. in A: no args. in C: no args. in B: no args. in A: no args. in C: no args. publicclass 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); } } publicclass D { publicstaticvoid main(String args[]) { C c = new C(); A a = new C(); } } 16

  17. ( 3 ) publicclass 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); } } publicclass B { public B() { System.out.println("in B: no args."); } } What is the output? : in B: no args. in A: no args. in C: s = c in B: no args. in A: no args. in C: s = a publicclass 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); } } publicclass D { publicstaticvoid main(String args[]) { C c = new C("c"); A a = new C( a"); } } 17

  18. . ( .) . . 18

  19. public class A { publicfloat foo(float a, float b) throws IOException { } } publicclass B extends A { } B ? - 1. float foo(float a, float b){ } 2. publicint foo(int a, int b) throws Exception{ } 3. publicfloat foo(float a, float b) throws Exception{ } 4. publicfloat foo(float p, float q) { } 19

  20. public class A { publicvoid foo() { } } publicclass B extends A { publicvoid foo() { } } foo - B ? A : super.foo() 20

  21. ( 2 ) public class A { publicvoid foo() { } } publicclass B extends A { publicvoid foo() { } } foo - C ? A , : super.super.foo() - publicclass C extends B { publicvoid foo() { } } 21

  22. public class Test { publicint a = 0; privateint b = 1; a-e - ? publicvoid foo(finalint c) { int d = 2; - d : class InnerTest { privatevoid bar(int e) { } } d = 3; a = 3; } } 22

  23. Scope - Interface Inner Type Fields access member no yes Only static Static nested member yes no Static and non-static Inner non- static Local scope yes no Effectively final local variables or parameters that are accessible in the scope of the block local Only the point where it is defined yes no Effectively final local variables or parameters that are accessible in the scope of the block anonymous 23

  24. enum All enums implicitly extend java.lang.Enum An enum cannot extend anything else. The constructor for an enum type is always private implicitly. You cannot invoke an enum constructor yourself. fixed set of constants 24

  25. 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

  26. : ) ( new HashSet<>(); 26

  27. HashSet Set HashSet . : HashSet ? " .1 Set ? ) ( HashSet .2 Set . 27

  28. HashSet . Set . , ? " ? , Collection . " 28

  29. ? func Java . . , toString 29

  30. " >?< . , Collection<Object> Collection<Object> set , 30

  31. " >?< . , Collection<Object> Collection<Object> set , 31

  32. 32

More Related Content