public class A {
public static void method(int x){
System.out.println("x = " + x);
}
}
public class B extends A {
public static void method(int y){
System.out.println("y = "+ y);
}
public static void method(String s){
System.out.println("s = "+s);
}
public static void main(String[] args){
A a1 = new A();
B a2 = new B();
a1.method(10);
a2.method(20);
}
}
x = 10
y = 20
in question paper no acces modifier is used. If don't use access modifier the program will not execute. If use access modifire such as public Exceptions
public class Exceptions{
public static void main(String[] args){
String languages[] = {"C", "C++", "Java", "Perl", "Python"};
try{
for(int c= 1; c<=5; c++){
System.out.println(languages[c]);
}
}catch(Exception e){
System.out.println(e);
}
}
}
C++
Java
Perl
Python
java.lang.ArrayIndexOutOfBoundsException: 5
There is an error in the question. Then we can handle the errors in the following
public class Exam {
public static void main(String[] args) {
int num1 = 30, num2 = 0, a[] = new int[10];
try{
int output1 = num1 / num2;
}catch(Exception e){
System.out.println("Cought exception while divide 30 by 0");
};
a[1] = 9;
int output2 = a[1] + num1;
System.out.println("Sum of array values = " + output2);
}
}
Cought exception while divide 30 by 0
Sum of array values = 39
public abstract class A {
public abstract void firstMethod();
public void secondMethod(){
System.out.println("SECOND");
firstMethod();
}
}
public abstract class B extends A {
public void abstract thirdMethod();
public void firstMethod(){
System.out.println("First");
thirdMethod();
}
}
public class C extends B {
public void thirdMethod(){
System.out.println("THIRD");
}
}
public class MainClass {
public static void main( String[] args ){
C c = new C();
c.firstMethod();
c.secondMethod();
c.thirdMethod();
}
}
FIRST
SECOND
THIRD