Predict the output of following Java Programs.
Program 1 :
Output: A B B A
Explanation: No magic of polymorphism here; the instance variable c from A is simply hidden in B
– a.c is ‘A’ because it is so set in the class A
– a.getValue() returns ‘B’ because the object is of type B
Program 2 :
Output: A A A A
Explanation: Concept of polymorphism for methods cannot be used here, since in class B there is no function overloading a method in class A. Refer run-time polymorphism for more details
Program 3 :
Adding to 100, x = 104 Adding to 0, y = 3 3 3
Explanation: Properties of static are shown in this example. 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. See more at: static keyword
Program 4 :
Explanation: Its legal to throw and handle errors and runtime exceptions. RuntimeException is a sub-subclass of Throwable. See exceptions for more details
Program 5 :
practice.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
This article is contributed by Pavan Gopal Rayapati. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.