
Understanding Java Interface and Exceptions
Learn about Java interfaces, exception handling, and class hierarchy through examples and image illustrations. Explore the concept of modifiers, class implementation, and more in this educational content.
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
: - java 9 ) public interface MyInterface { ( , public abstract int foo1(int i); int foo2(int i); } The modifiers of foo1 and foo2 are the same. 3
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
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(); } : 5 "Unhandled exception type Exception"
- 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(); } ? , ? ? ? ? , , , 6
- 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(); } : 7 "No exception is thrown"
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 = new Poodle(); Animal animal = (Animal) poodle; Dog dog = new Labrador(); animal = dog; poodle = dog; 8
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; Poodle poodle = new Poodle(); Animal animal = (Animal) poodle; Dog dog = new Labrador(); animal = dog; poodle = dog; Error Type mismatch: cannot convert from Dog to Poodle -No compilation error -Runtime Exception - Compilation Labrador labrador = (Labrador) dog; -No compilation error -No Runtime Exception 9
class A { publicvoid print() { System.out.println("A"); } } class B extends A implements C { } ? interface C { void print(); } 10
class A { publicvoid print() { System.out.println("A"); } } class B extends A implements C { } interface C { void print(); } public 11
class A { System.out.println("A"); } } class B extends A implements C { } void print() { ? interface C { void print(); } 12
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 13
The following table shows the access to members permitted by each modifier Access Level Modifier public protected No modifier private Class Y Y Y Y Package Y Y Y N Subclass Y Y N N World Y N N N default 14 https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
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; b.print(); a.print(); } } ? , ? ? ? ? , , , 15
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 ? , , , 16
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"); } } } ? , ? ? ? ? , , , 17
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" : ? , ? ? ? ? , , , 18
( 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"); } } } ? , ? ? ? ? , , , 19
( 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 20
( 3 ) publicclass A { public void foo() { System.out.println("A"); } } publicclass B extends A { public static void foo() { System.out.println("B"); } } public class BindingTest { public static void main(String args[]) { B b = new B(); b.foo(); } } , ? ? 21 ? ,
( 3 ) publicclass A { public void foo() { System.out.println("A"); } } Compilation Error: foo() in B cannot override foo() in A. publicclass B extends A { public static void foo() { System.out.println("B"); } overriding method is static. } public class BindingTest { public static void main(String args[]) { B b = new B(); b.foo(); } } , ? ? 22 ? ,
( 4 ) publicclass A { public static void foo() { System.out.println("A"); } } publicclass B extends A { public void foo() { System.out.println("B"); } } public class BindingTest { public static void main(String args[]) { B b = new B(); b.foo(); } } , ? ? 23 ? ,
( 4 ) publicclass A { public static void foo() { System.out.println("A"); } } Compilation Error: foo() in B cannot override foo() in A. publicclass B extends A { public void foo() { System.out.println("B"); } overridden method is static. } public class BindingTest { public static void main(String args[]) { B b = new B(); b.foo(); } } , ? ? 24 ? ,
( 5 ) publicclass A { private static void foo() { System.out.println("A"); } } publicclass B extends A { public void foo() { System.out.println("B"); } } public class BindingTest { public static void main(String args[]) { B b = new B(); b.foo(); } } , ? ? 25 ? ,
( 5 ) publicclass A { private static void foo() { System.out.println("A"); } } Output: B publicclass B extends A { public void foo() { System.out.println("B"); } } public class BindingTest { public static void main(String args[]) { B b = new B(); b.foo(); } } , ? ? 26 ? ,
Static binding (or early binding) Static binding: bind at compilation time Performed if the compiler can resolve the binding at compile time Applied for Static methods Private methods Final methods Fields 27
Binding public class B extends A { public void foo() { System.out.println("B.foo()"); } public class A { public void foo() { System.out.println("A.foo()"); } public void bar() { System.out.println("A.bar()"); foo(); } } public static void main(String[] args) { A a = new B(); a.bar(); } } ? ? ? , , ? , , 28 ?
Binding public class B extends A { public void foo() { System.out.println("B.foo()"); } public class A { public void foo() { System.out.println("A.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() ? ? ? , , ? , ? , 29
Binding (2) public class B extends A { public void foo() { System.out.println("B.foo()"); } public class A { private void foo() { System.out.println("A.foo()"); } public void bar() { System.out.println("A.bar()"); foo(); } } public static void main(String[] args) { A a = new B(); a.bar(); } } ? , ? ? ? ? , , , 30
Binding (2) public class B extends A { public void foo() { System.out.println("B.foo()"); } public class A { private void foo() { System.out.println("A.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() 31
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) { } 40
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) { } 41
public class A { publicvoid foo() { } } publicclass B extends A { publicvoid foo() { } } foo - B ? A : super.foo() 42
( 2 ) public class A { publicvoid foo() { } } publicclass B extends A { publicvoid foo() { } } foo - C ? A , : super.super.foo() - publicclass C extends B { publicvoid foo() { } } 43
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 47
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 48
Set HashSet . HashSet " ? : HashSet .1 Set ) ? ( HashSet .2 Set . 50