- Binding of variables
class A {
private void f() {
System.out.println("A f");
}
}
class B extends A {
public void f() {
System.out.println("B f");
}
}
class Main {
public static void main(String[] args) {
B b = new B();
A a = b;
a.f(); //this will throw an error
b.f();
}
}
class A {
void f() {
System.out.println("A f");
}
}
class B extends A {
void f() {
System.out.println("B f");
}
}
B b = new B();
b.f(); //B f
A a = b;
a.f(); //B f
a = new A();
a.f(); //A f
void f() {
System.out.println("A f");
}
}
class B extends A {
void f() {
System.out.println("B f");
}
}
B b = new B();
b.f(); //B f
A a = b;
a.f(); //B f
a = new A();
a.f(); //A f
The second program doesn't throw an error. The reason for this is due to the fact that when calling a in the second program, since its bound to b, we will realise that class B extended and override the f method. Thus it calls the B's overridden method
However, in the first program, it will throw an error since f in a is private, meaning that b could not override A. Binding A a = b and calling f from a will throw an error since f() does not exist for B to override.
Given another example whereby we add another method g() in B. Using the bounded A a = b, where b is an object of class B, we cannot call a.g() since g() does not exist in A.
2. Exceptions
We use the throws keyword to pass the responsibility of exception handling to the caller. The caller method is responsible to handle that exception.
When throwing a new exception, the method either have to use a try-catch method or method throws the exception for the calling method to handle it.
Unreachable code:
class Main {
static void f() throws IllegalArgumentException {
try {
System.out.println("Before throw");
throw new IllegalArgumentException();
System.out.println("After throw"); //unreachable
} catch (IllegalArgumentException e) {
System.out.println("Caught in f");
}
}
public static void main(String[] args) {
try {
System.out.println("Before f");
f();
System.out.println("After f");
} catch (Exception e) {
System.out.println("Caught in main");
}
}
}
static void f() throws IllegalArgumentException {
try {
System.out.println("Before throw");
throw new IllegalArgumentException();
System.out.println("After throw"); //unreachable
} catch (IllegalArgumentException e) {
System.out.println("Caught in f");
}
}
public static void main(String[] args) {
try {
System.out.println("Before f");
f();
System.out.println("After f");
} catch (Exception e) {
System.out.println("Caught in main");
}
}
}
This will not run as there is an unreachable code. By throwing an exception, there print fn will never be reach
Another example of unreachable code:
static void f() throws Exception {
try {
throw new ArrayIndexOutOfBoundsException();
} catch (Exception e) {
System.out.println("Caught exception in f");
} catch (ArrayIndexOutOfBoundsException e) { //unreachable
System.out.println("Caught AIOOB exception in f");
}
}
try {
throw new ArrayIndexOutOfBoundsException();
} catch (Exception e) {
System.out.println("Caught exception in f");
} catch (ArrayIndexOutOfBoundsException e) { //unreachable
System.out.println("Caught AIOOB exception in f");
}
}
Since the Exception e is caught, it is the mother of all exception thus it will catch all exception. The catching of arrayindexoutofbounds will be caught there instead of the last catch method thus its unreachable.
Unfortunately, Java knows that the moment you throw an exception like the example below, it will not compile because it will complain that it is unreachable
- public static void Main throws Exception{
- try{
- throw new Exception();
- System.out.println("owo"); //this is also unreachable
- }
- catch (Exception e){
- }
- }
- public static void Main throws Exception{
- try{
- f();
- System.out.println("owo");
- }
- catch (Exception e){
- }
- }
- public void f() throws Exception{
- int i = 9/0; //definitely throw an exception
- }
The exception is the mother of all exception thus throwing it or method throws it will ensure that all methods will be thrown. Catching Exception will ensure every exception thrown will be caught however it is not good programming practice.
<Prev Generics Next Stacks and Heaps>
No comments:
Post a Comment