Java inheritance questions

Advertisements

I’m new to java and trying to solve this question. I need to answer the ??? part in the code to make it returns true.

I cannot parse the syntax of class A. What’s the String name; at the end of class A ?

class A{A(String name){this.name=name;}String name;}
class B extends A{[???]}
public class Exercise{
  public static void main(String [] arg){
    assert (new B().name.equals("Cell"));
  }
}

>Solution :

You can add a constructor in B, which calls its parent’s constructor.

B() {
   super("Cell");
}

And the following is the complete code:

class A {
    String name;
    A(String name) {
        this.name = name;
    }
}
class B extends A {
    B() {
        super("Cell");
    }
}
public class Exercise {
    public static void main(String[] args) {
        assert (new B().name.equals("Cell"));
    }
}

Leave a ReplyCancel reply