Consider the classes below, declared in the same file:
class A {
int a;
public A() {
a = 7;
}
}
class B extends A {
int b;
public B() {
b = 8;
}
What is wrong in the code below:
1. A obj = new A();
System.out.println(obj.b);
2. B obj = new B(10);
System.out.println(obj.b);
3. write java code segment that create a class C and C class inherits from B. The C class
contain an integer variable c and assign its value using default constructor.
4. create an instance for C and display the value of a, b, c
5. Suppose A is a class, B is a subclass of A, and both A and B have a no-argument constructor. Write java code to create an instance of B using A class type.
6. What is the output of running class Test?
public class Test {
public static void main(String[] args) {
new Circle9();
}
}
public class GeometricObject {
GeometricObject() {
System.out.print("A");
}
GeometricObject(String color, boolean filled) {
System.out.print("B");
}
}
public class Circle9 extends GeometricObject {
/** No-arg constructor */
public Circle9() {
this(1.0);
System.out.print("C");
}
/** Construct circle with a specified radius */
public Circle9(double radius) {
this(radius, "white", false);
System.out.print("D");
}
/** Construct a circle with specified radius, filled, and color */
public Circle9(double radius, String color, boolean filled) {
super(color, filled);
System.out.print("E");
}
}
Consider the following code:
public class Test {
public static void main(String[] args) {
Object a1 = new A();
Object a2 = new A();
System.out.println(a1);
System.out.println(a2);
}
}
class A {
int x;
public String toString() {
return "A's x is " + x;
}
}
7. What is the output of above code
In the 2nd line there is no property in class A with the name b.
There is no constructor in class B that has 1 integer parameter
C obj=new C();
System.out.println(obj.a+”, ”+obj.b+”, ”+obj.c);
A obj=new B();
Output:
BEDC