Thursday, April 30, 2009

Puzzle 20 - Fill in the code

Language – Java | Type – Concept | Last date 03-May-2009 12:00 p.m. IST | Points 2

Just simply fill in the missing code ...
package com.twisters;
class Base {
public int a;

public int getA(){
return a;
}
}
public class Child extends Base{
private int a;

Child(
int value){
/* Code goes here */
}

public static void main(String[] args) {
Base b
= new Child(10);
System.out.println(b.getA());
//This should print 10
}
}

Got an answer? Why don’t you leave it here.


3 comments:

  1. package com.twisters;
    public class Child extends Base{
    private int a;

    Child(int value){
    super.a = value;
    }

    public static void main(String[] args) {
    Base b = new Child(10);
    System.out.println(b.getA()); //This should print 10
    }
    }

    ReplyDelete
  2. super.a = value;

    ReplyDelete
  3. the code raises a compilation error
    because without a Base class constructor you can not define Child class Constructor

    nsureshreddy@live.com

    ReplyDelete

Solution for this question?