Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I am just learning java
here is my code : I have to add code [???] line

//The answer must have balanced parentesis
class A0{private int f;A0(int f){this.f=f;}
  int getF(){return f;}
  }
class A1 extends A0{private int f=0;A1(int f){super(f);}
  int getF(){return f;}
  }
class B extends A1{
  private int f=0;
  [???]
  }
public class Exercise{
  public static void main(String[]arg){

    B b7=new B(7);
 
 
    assert b7.getFofA()==7;
 
  }
}


What I have tried:

 B(int f) {
     super(f);
     System.out.print("B constructor called \n");
 }
 public int getFofA() {
// if I do super.f then it will just give me 0 as it calls the one level up class's f

 }
Posted
Updated 23-Jul-22 10:15am

1 solution

class A0 {
  private int f=10;

  A0( int f) { this.f=f; }
  int getF() { return f; }
}

class A1 extends A0 {
  private int f=11;
  
  A1( int f) { super(f); }
  
  public int getSuperF() {
    return super.getF();
  }
}

class B extends A1 {
  private int f=12;
  
  B( int f)  { super(f); }
  int getF() { return f; }
  
  /* public int getSuperF() {
    return super.getSuperF();
  } */
}

public class Exercise {
  public static void main(String[]arg) {
    B b = new B(7);
    System.out.println( b.getF() );
    System.out.println( b.getSuperF() );
  }
}
 
Share this answer
 
Comments
Member 4201820 23-Jul-22 22:08pm    
Thanks, but, we can not change the main()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900