SCJP Module-4 Question-12

The program given below checks your knowledge of method overriding and Inheritance in Java.

 
 

Share on Google+Share on Google+

SCJP Module-4 Question-12

Ads

The program given below checks your knowledge of method overriding and Inheritance in Java.

Given below the sample code :

class Family

{  int x, y;
   Family(int x, int y)
{  this.x = x;
   this.y = y;
}
public int addMeAsRelative(int x, int y)
{
return this.x + x + y + this.y;
}
public int addMeAsRelative(Family fam)
{
return addMeAsRelative(fam.x, fam.y);
}}


class MyChild extends Family
{
int z;
MyChild(int x, int y, int z)
{
super(x, y);
this.z = z;
}
public int addMeAsRelative(int x, int y, int z)
{
return this.x + x + this.y + y + this.z + z;
}
public int addMeAsRelative(MyChild myChi)
{
return addMeAsRelative(myChi.x, myChi.y, myChi.z);
}
public int addMeAsRelative(int x, int y)
{
return this.x + x + this.y + y;
}}

public class MySomeOne
{
public static void main(String args[])
{
MyChild myChi = new MyChild(10, 20, 30);
Family fam = new Family(10, 20);
int x = myChi.addMeAsRelative(10, 20, 30);
int y = myChi.addMeAsRelative(myChi);
int z = fam.addMeAsRelative(fam);
System.out.println(x + y + z);
}}

What will be the output of the following code:

1. 120
2. 240
3. 300
4. 180
5. Compilation Error
6. None of the above

Answer :

(3)

Advertisements

Ads

Posted on: July 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles