4) - Show the output of following program: 1 public class Test { 2 public static void main(String[] args) { 3 A a = new
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
4) - Show the output of following program: 1 public class Test { 2 public static void main(String[] args) { 3 A a = new
5) - Show the output of following program:public class Test {public static void main(String[] args) {new A();new B();}}class A {int i = 7;public A() {setI(20);System.out.println("i from A is " + i);}public void setI(int i) {this.i = 2 * i;}}class B extends A {public B() {System.out.println("i from B is " + i);}public void setI(int i) {this.i = 3 * i;}}
6) - What is wrong in the following code?1 public class Test {2 public static void main(String[] args) {3 Object fruit = new Fruit();4 Object apple = (Apple)fruit;5 }6 }78 class Apple extends Fruit {9 }1011 class Fruit {12 }
2) Show the output of the following code public class Test { public static void main(String[] args) { new Person().printPerson (): new Student ().printPerson(); } } class Student extends Person { @Override public String getInfo() { return "Student": } } class Person { public String getInfo() { return "Person"; } public void printPerson() { System.out.println(getInfo()); } (a) public class Test { public static void main(String[] args) { } class Student extends Person { private String getInfo() { return "Student"; } } } class Person { private String getInfo() { return "Person"; new Person().print Person (): new Student ().printPerson(); } public void print Person () { System.out.println (getInfo(); } (b)
3) What is the output of running the class in (a)? What problem arises in compiling the program in (b)? class A [ public A() { System.out.println( "A's no-arg constructor is invoked"); class B extends A { public class C { public static void main(String[] args) { B b = new B(); (a) class A { public A(int x) { |} class B extends A { public B() { public class C { public static void main(String[] args) { B b = new B(); } (b)