Output of Java Program | Set 19
Last Updated :
21 Apr, 2025
Predict the output of the following Java Programs.
Program 1: Runtime Polymorphism with Hidden Variables and Overridden Methods
Java
public class RuntimePolymorphism
{
public static void main(String[] args)
{
A a = new B();
B b = new B();
System.out.println(a.c + " " + a.getValue()
+ " " + b.getValue() + " " + b.getSuperValue());
}
}
class A
{
protected char c = 'A';
char getValue()
{
return c;
}
}
class B extends A
{
protected char c = 'B';
char getValue()
{
return c;
}
char getSuperValue()
{
return super.c;
}
}
Explanation: Even a refers to a B object, accessing a.c uses the variable from class A due to variable hiding. But, a.getValue() calls the overridden method in B. It shows how runtime polymorphism works for methods but not for variables.
Program 2: No Method Overriding
Java
public class RuntimePolymorphism
{
public static void main(String[] args)
{
A a = new B();
B b = new B();
System.out.println(a.c + " " + a.getValue() +
" " + b.getValue() + " " + b.getSuperValue());
}
}
class A
{
protected char c = 'A';
char getValue()
{
return c;
}
}
class B extends A
{
protected char c = 'B';
char getSuperValue()
{
return super.c;
}
}
Explanation: Concept of polymorphism for methods cannot be used here, since in class B does not override getValue(), a.getValue() and b.getValue() both call the inherited method from A. Also, the variable c in B hides the one in A, but method calls are unaffected here. Refer run-time polymorphism for more details.
Program 3: Static Variables Shared Across Instances
Java
class test
{
public static int y = 0;
}
class HasStatic
{
private static int x = 100;
public static void main(String[] args)
{
HasStatic hs1 = new HasStatic();
hs1.x++;
HasStatic hs2 = new HasStatic();
hs2.x++;
hs1 = new HasStatic();
hs1.x++;
HasStatic.x++;
System.out.println("Adding to 100, x = " + x);
test t1 = new test();
t1.y++;
test t2 = new test();
t2.y++;
t1 = new test();
t1.y++;
System.out.print("Adding to 0, ");
System.out.println("y = " + t1.y + " " + t2.y + " " + test.y);
}
}
OutputAdding to 100, x = 104
Adding to 0, y = 3 3 3
Explanation: Properties of static are shown in this example. Static variables x and y are shared across all instances. When a variable is declared as static, then a single copy of variable is created and shared among all objects at class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. To know more, refer to this article static keyword.
Program 4: Nested try-catch with Throwable
Java
public class Except
{
public static void main(String[] args)
{
try
{
throw new Error();
}
catch (Error e)
{
try
{
throw new RuntimeException();
}
catch (Throwable t)
{
}
}
System.out.println("phew");
}
}
Explanation: Its legal to throw and handle errors and runtime exceptions. Here, Throwable is the superclass of both Error and RuntimeException, so it catches the inner thrown exception. RuntimeException is a sub-subclass of Throwable. See exceptions for more details.
Program 5: Static and Instance Initializers
Java
public class Boot
{
static String s;
static
{
s = "";
}
{
System.out.println("GeeksforGeeks ");
}
static
{
System.out.println(s.concat("practice.GeeksforGeeks "));
}
Boot()
{
System.out.println(s.concat("Quiz.GeeksforGeeks"));
}
public static void main(String[] args)
{
new Boot();
System.out.println("Videos.GeeksforGeeks");
}
}
Outputpractice.GeeksforGeeks
GeeksforGeeks
Quiz.GeeksforGeeks
Videos.GeeksforGeeks
Explanation: static init blocks run before instance init blocks (in the order in which they appear, respectively). The order of initialization constructors and initializer block doesn’t matter, initializer block is always executed before constructor. See static block for details.