Q:

oop inheritence in java , Consider the classes below, declared in the same file:

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

  1. In the 2nd line there is no property in class A with the name b.

  2. There is no constructor in class B that has 1 integer parameter

  3. class C extends B
    {
    int c;
    public C()
    {
    c=0;
    }
    }
  4. C obj=new C();

    System.out.println(obj.a+”,  ”+obj.b+”,  ”+obj.c);

  5. A obj=new B();

  6. Output:

    BEDC

  7. A’s x is null

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now