Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another mock exam question.

 
Makarand Akdar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be the output when you compile and execute the following program.

////////////////////////////////////////////
class Base
{
void test() {
System.out.println("Base.test()");
}

}
////////////////////////////////////////////
public class Child extends Base {

void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {
Child anObj = new Child();
Base baseObj = (Base)anObj;
baseObj.test();
}
}

Select most appropriate answer.

a) Child.test()
Base.test()
b) Base.test()
Child.test()
c) Base.test()
d) Child.test()

The answer given is d)
I think c) is the right answer.
Please help.
-Thanks
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
methods are bound at runtime and variables are bound at compile time. This type of questions are repeated many times in this forum so i encourage you to go few days back in the forum and check out the threads!!

 
Sri Enga
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I compile the above mentioned code and run it gives me the following
Exception in thread "main" java.lang.NoClassDefFoundError : Child/java
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,
1) Child chObj=new Child();
2) Base basObj=(Base)chObj;
Concept Applied : Methods are related to objects and variables are related to classes.
So, here, by invoking the method basObj.test(), will execute that method which belongs to the Child class, as it's object is being created in line (1), and the variables (if any are there), will be used from the Base class, as it's class is being used.
I may sound a bit confusing, but I think am right!!
Can Anybody confirm this!
Bye,
Tualha Khan
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Ans definitely should be d not c. Because you are making an obj of subclass and assigning a copy of reference to an obj of subclass to the super class where type is super i.e
Base baseobj=(Base)anobj; //cast anobj to the superclass is ok
//at compile time and it is also ok
//at run time because Parent goes inside
//the kids, kids can not go inside the
//Parent. So here Base goes inside the
//subclass and the test() method of
// subclass is override the test()
// method of Base class.

Also read the following type casting example.
TestCast.java
-----------------------------------------------------------------
public class TestCast {
public static void main (String[] args){

Cat aCat = new Cat();
Seal aSeal = new Seal();
FourLegs a4leg;
SeaMammal aSeaMammal;
Mammal aMammal;
Animal aAnimal;
aMammal = aSeal; // aSeal is a Mammal
a4leg = aCat; // aCat is a FourLegs
aAnimal = aMammal; // aMammal is a Animal
aSeaMammal = aSeal; // aSeal is a SeaMammal
//a4leg = aSeal; // aSeal is not a FourLegs
//aSeal = (Seal)aCat; // cannot cast to sibling class even w/a
//cast
// The following 4 are examples of improper casting
// They will succeed at compile time, and throw
// RuntimeExceptions.

// When running the program, you only can see one at a time.
// Commenting out the previous one(s)
// you will see the next RuntimeException.
a4leg = (FourLegs)aSeal;// cast obj to an interface is ok at
// compile time
// will throw ClassCastException at run time.
// since aSeal is not a FourLegs
aSeal = (Seal)a4leg; // cast interface to an obj is ok at
// compile time
// will throw ClassCastException at run time.
// since FourLegs cannot be a seal.

aCat = (Cat)aAnimal; // cast superclass to its subclass is ok
// at compile time.
// will throw ClassCastException at run time.
// since aAnimal is actually a Seal, not a Cat

aSeaMammal = (SeaMammal)a4leg; // cast between irrelavent
//interfaces is ok
// at compile time,
// will throw ClassCastException
// at run time.
// since cat is not a SeaMammal
}
}

class Animal {
}
class Mammal extends Animal{
}
class Whale extends Mammal implements SeaMammal{
}
class Seal extends Mammal implements SeaMammal{
}
class Cat extends Mammal implements FourLegs{
}
class Rat extends Mammal implements FourLegs{
}
interface FourLegs {
}
interface SeaMammal {
}

-----------------------------------------------------------------
Hope it should clear it out
- Golam Newaz
 
Jonathan Li
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's a tricky question.
think of it this way. the explicit (Base) cast is not needed. without it, the actual object type is Child...so the Child's method is run.
the cast only says that the reference is of type Base (which it is anyway with or without the cast)...the actual object at runtime is indeed instanceof Child...dynamic binding then kicks in, and the rest is history.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic