Java Interface Modifiers in Class Hierarchy
In this content, we explore Java interface modifiers, class hierarchy, and exception handling. The examples illustrate concepts like abstract methods, interface implementation, and class extension. Learn about the relationships between Animal, Dog, Poodle, and Labrador classes in Java programming. Understand the scenarios where exceptions are thrown and how they are handled.
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 : Summery 1
2 ! :
3 : java 9 ) - , ( public interface MyInterface { public abstract int foo1(int i); int foo2(int i); } The modifiers of foo1 and foo2 are the same.
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 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(); } } : "Unhandled exception type Exception"
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 - 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(); } } : "No exception is thrown"
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 = new Poodle(); Animal animal = (Animal) poodle; Dog dog = new Labrador(); animal = dog; poodle = dog;
9 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; Type mismatch: cannot convert from Dog to Poodle -No compilation error -Runtime Exception - Compilation Error Labrador labrador = (Labrador) dog; -No compilation error -No Runtime Exception
10 class A { public void print() { System.out.println("A"); } } ? class B extends A implements C { } interface C { void print(); }
11 class A { public void print() { System.out.println("A"); } } class B extends A implements C { } interface C { void print(); } public
12 class A { void print() { System.out.println("A"); } ? } class B extends A implements C { } interface C { void print(); }
13 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
14 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 https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
15 public class A { public void print() { System.out.println("A"); } } public class C { public static void main(String[] args){ B b = new B(); A a = b; b.print(); a.print(); public class B extends A { public void print() { System.out.println("B"); } } } } ? ? ? ? , ? , , ,
16 public class A { public void print() { System.out.println("A"); } } public class C { public static void main(String[] args){ B b = new B(); A a = b; - casting b.print(); a.print(); public class B extends A { public void print() { System.out.println("B"); } } } } : ? ? ? ? , B B ? , , ,
17 public class A { public void print() { System.out.println("A"); } } public class C { public static void main(String[] args) { B b = new B(); b.print(); } public class B extends A { protected void print() { System.out.println("B"); } } } ? ? ? ? , ? , , ,
18 public class A { public void print() { System.out.println("A"); } } public class C { public static void main(String[] 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" : ? ? ? ? , ? , , ,
19 ( 2 ) public class A { protected void print() { System.out.println("A"); } } public class C { public static void main(String[] args) { B b = new B(); b.print(); } public class B extends A { public void print() { System.out.println("B"); } } } ? ? ? ? , ? , , ,
20 ( 2 ) public class A { protected void print() { System.out.println("A"); } } public class C { public static void main(String[] args) { B b = new B(); b.print(); } public class B extends A { public void print() { System.out.println("B"); } } } ? ? ? ? , : ? , , , B
21 ( 3 ) public class A { public void foo() { System.out.println("A"); } } public class 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(); } } ? , ? ? ,
22 ( 3 ) public class A { public void foo() { System.out.println("A"); } } Compilation Error: foo() in B cannot override foo() in A. overriding method is static. public class 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(); } } ? , ? ? ,
23 ( 4 ) public class A { public static void foo() { System.out.println("A"); } } public class 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(); } } ? , ? ? ,
24 ( 4 ) public class A { public static void foo() { System.out.println("A"); } } Compilation Error: foo() in B cannot override foo() in A. overridden method is static. public class 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 ) public class A { private static void foo() { System.out.println("A"); } } public class 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 ( 5 ) public class A { private static void foo() { System.out.println("A"); } } Output: public class B extends A { public void foo() { System.out.println("B"); } B } public class BindingTest { public static void main(String args[]) { B b = new B(); b.foo(); } } ? , ? ? ,
27 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
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(); } } } ? ? ? ? , ? , , ,
29 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() ? , , ,
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(); } } } ? ? ? ? , ? , , ,
31 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()
32 . ( .) . .
public class B extends A{ String bar = B.bar ; B() { foo(); } } B(){ bar = null; super(); bar = B.bar ; foo(); } . ( .) .1 .2 .3 . .4 . 33
34 public class A { String bar = "A.bar"; public class C { public static void main(String[] args) { A a = new B(); System.out.println("a.bar = " + a.bar); a.foo(); } } A() { foo(); } public void foo() { System.out.println("A.foo(): bar = " + bar); } } public class B extends A { String bar = "B.bar"; ? B() { foo(); } public void foo() { System.out.println("B.foo(): bar = " + bar); } }
35 public class A { String bar = "A.bar"; public class C { public static void main(String[] args) { A a = new B(); System.out.println("a.bar = " + a.bar); a.foo(); } } A() { foo(); } public void foo() { System.out.println("A.foo(): bar = " + bar); } } public class 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 public void foo() { System.out.println("B.foo(): bar = " + bar); } }
36 ( 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); } } What is the output? A public class B { public B() { System.out.println("in B: no args."); } } B 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); } } C public class D { public static void main(String args[]) { C c = new C(); A a = new C(); } }
37 ( 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); } } What is the output? : A in B: no args. in A: no args. in C: no args. in B: no args. in A: no args. in C: no args. public class B { public B() { System.out.println("in B: no args."); } } B 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); } } C public class D { public static void main(String args[]) { C c = new C(); A a = new C(); } }
38 ( 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); } } What is the output? 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"); } }
39 ( 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); } } 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 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"); } }
40 public class A { public float foo(float a, float b) throws IOException { } } public class B extends A { } B ? - 1. float foo(float a, float b){ } 2. public int foo(int a, int b) throws Exception{ } 3. public float foo(float a, float b) throws Exception{ } 4. public float foo(float p, float q) { }
41 public class A { public float foo(float a, float b) throws IOException { } } public class B extends A { } B ? - 1. float foo(float a, float b){ } 2. public int foo(int a, int b) throws Exception{ } 3. public float foo(float a, float b) throws Exception{ } 4. public float foo(float p, float q) { }
42 public class A { public void foo() { } } foo - B ? A : super.foo() public class B extends A { public void foo() { } }
43 ( 2 ) public class A { public void foo() { } } foo - C ? A , : public class B extends A { public void foo() { } } super.super.foo() - public class C extends B { public void foo() { } }
44 public class Test { public int a = 0; private int b = 1; a-e - ? public void foo(final int c) { int d = 2; class InnerTest { private void bar(int e) { } } d = 3; a = 3; } }
45 public class Test { public int a = 0; private int b = 1; a-e - ? public void foo(final int c) { int d = 2; - d : class InnerTest { private void bar(int e) { } } d = 3; a = 3; } }
46 - Type Construct Interface Fields access Anywhere yes Only static Static nested Anywhere, From outer class member Local scope (only in the function it was declared) Only the point where it is defined no Static and non-static Inner non- static no Effectively final local variables or parameters that are accessible in the scope of the block Effectively final local variables or parameters that are accessible in the scope of the block local no anonymous
47 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
48 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
49 : new HashSet<>(); ) (
50 HashSet Set HashSet . : HashSet ? " .1 Set ? ) ( HashSet .2 Set .