Wednesday, 29 July 2020
JSON Tutorial
Introduction to Java
Over time new enhanced versions of Java have been released. The current version of Java is Java 10, which was released on 20 March 2018.
The Java language was designed with the following properties:
- Simple
- Platform independent:
- Object-orientated programming language:
- Portable
- Robust
- Secured
- Multi threading
4 main concepts of Object Oriented programming are:
Tuesday, 28 July 2020
Exception handling in Method Overriding
package com.tutorial4u;
class A {
void m1() {
System.out.println("method A");
}
}
class B extends A {
void m1() throws NullPointerException {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : Method B
Case 2 : If superclass doesn’t throw any exception but child class throws an checked exception
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() {
System.out.println("method A");
}
}
class B extends A {
// Exception IOException is not compatible with throws clause in A.m1()
void m1() throws IOException {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : Exception in thread "main" java.lang.Error: Unresolved compilation problem: Exception IOException is not compatible with throws clause in A.m1()
Note : We cannot throw a checked exception if the overridden method(method of base class) is not throwing an exception.
Case 3 : If superclass throws checked exception but subclass throws narrower (subclass of) checked exception
package com.tutorial4u;
import java.io.FileNotFoundException;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
void m1() throws FileNotFoundException {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) throws IOException {
A a = new B();
a.m1();
}
}
Output : method B
Case 4 : If superclass throws checked exception but subclass throws broader (superclass of ) checked exception.
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
// Exception
Exception is not compatible with throws clause in A.m1()
void m1() throws Exception {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
try {
a.m1();
} catch (Exception
e) {
e.printStackTrace();
}
}
}
Output :
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Exception Exception is not compatible with throws clause in A.m1()
Note : Getting compilation error because the m1() method of child class is throwing Exception which has a broader scope than the exception thrown by method m1() of parent class.
But if child class method have child exception of the super class declared exception or same or not throwing any exception. It will execute fine.
Case 5 : If superclass throws checked exception but subclass throws an unchecked Exception (RuntimeException)package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
// Exception Exception is not compatible with throws clause in A.m1()
void m1() throws ArithmeticException {
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
try {
a.m1();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output : Method B
Case 6 : If superclass throws unchecked Exception but subclass throw a checked exception.
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws ArithmeticException {
System.out.println("method A");
}
}
class B extends A {
// Exception
IOException is not compatible with throws clause in A.m1()
void m1() throws IOException{
System.out.println("Method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
try {
a.m1();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output :
package com.tutorial4u;
class A {
void m1() throws ArithmeticException {
System.out.println("method A");
}
}
class B extends A {
void m1() throws NullPointerException
{
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : method B
package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
void m1() throws IOException {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) throws IOException {
A a = new B();
a.m1();
}
}
Output : method B
Case 9 : if superclass throws a checked exception but subclass does not throw any exception.package com.tutorial4u;
import java.io.IOException;
class A {
void m1() throws IOException {
System.out.println("method A");
}
}
class B extends A {
void m1() {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) throws IOException {
A a = new B();
a.m1();
}
}
Output : method B
Case 10 : If superclass throws an unchecked exception but subclass doesn't throw any exception
package com.tutorial4u;
class A {
void m1() throws ArithmeticException {
System.out.println("method A");
}
}
class B extends A {
void m1() {
System.out.println("method B");
}
}
public class OverridingExample {
public static void main(String args[]) {
A a = new B();
a.m1();
}
}
Output : method B
No of Occurrence in Array
package com.tutorial4u; import java.util.HashMap; /************************************************************ No of Occurrence in Array...
-
What is JVM ? Java virtual Machine(JVM) is a virtual Machine that provides runtime environment to execute java byte code. The JVM doesn...