aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another mock exam question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Another mock exam question." Watch "Another mock exam question." New topic
Author

Another mock exam question.

Makarand Akdar
Greenhorn

Joined: Feb 03, 2001
Posts: 3
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

Joined: Feb 01, 2001
Posts: 130
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!!



Sun Certified Programmer for Java 2 Platform
Sri Enga
Ranch Hand

Joined: Feb 02, 2001
Posts: 51
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

Joined: Nov 22, 2000
Posts: 287
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


SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
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

Joined: Feb 05, 2001
Posts: 4
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another mock exam question.
 
Similar Threads
Casting question
call to shadowed static method at runtime!!!
Constructor
Confused help me!!
Overriding