File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overriding question" Watch "Overriding question" New topic
Author

Overriding question

Harsh Pathak
Greenhorn

Joined: May 09, 2006
Posts: 4
Hi,

I found this sample question for SCJP 1.4 on the site www.javacertificate.com :

class Phone {
2. static String device = "Phone.device";
3. void showDevice() {
4. System.out.print("Phone.showDevice," + device + " ");
5. }
6. Phone() {
7. showDevice();
8. }
9. }

1. class Mobile extends Phone {
2. String device = "Mobile.device";
3. void showDevice() {
4. System.out.print("Mobile.showDevice," + device + " ");
5. }
6. Mobile() {
7. showDevice();
8. }
9. public static void main(String[] args) {
10. Mobile n = new Mobile();
11. n.showDevice();
12. }
13. }

CHOICES:
1 Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

2 Phone.showDevice,Phone.device Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

3 Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device

4 Compile Time error

5 RunTimeException is thrown

The correct answer is 3. Can anyone explain why? I am not able to get it.

Thanks,
Harsh.
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
package pack23;

class Phone {

static String device = "Phone.device";

void showDevice() {

System.out.print("Phone.showDevice," + device + " ");
}

Phone() {

showDevice();
}
}

class Mobile extends Phone {

String device = "Mobile.device";

void showDevice() { // line (2)

System.out.print("Mobile.showDevice," + device + " ");
}

Mobile() {
// super(); First line: implicitly added by compiler, call to Phone'e constructor. // line (0)
// device is initilized now.
showDevice();
}

public static void main(String[] args) {

Mobile n = new Mobile(); // line (1)
n.showDevice(); // line (3)
}
}

/*
Well that's a good Question.

Read and understand each and every points before going to next one.
At the end everything makes sense.

1) Here there are two classes Phone(superclass) and Mobile(subclass).
This is how object initialization occures when you create an instance of subclass(ie Mobile).

First line of Child Constructor (invoked by "new")
First line of Child Constructor (invoked by "this")
First line of Parent Constructor(call to its superclass constructor,Object here)
Parent Class Initializers in the order of definition.
Remainder of Parent Constructor
Child Class Initializers in the order of definition.
Remainder of Child Constructor (the one invoked by "this")

2) Method calls to Non-final instance methods are mapped at run-time, depending on the instance to which it is pointing.
When the method is overriden in subclass(showDevice in this case), overriden method is called(Dynamic look-up behaviour)

3) When you create instance of Mobile in line (1)

a) With out initializing device reference in Mobile class call is forwarded to constructor of Phone class(refer point 1).
b) Now when showDevice() is called from Phone's constructor method in line (2) is called(Refer point 2).
c) In the call to showDevice() in Mobile's constructor, once again method in line (2) is called.
d) Call to showDevice() in line (3) calls method in line (2).

In line (0) implicitly instance of Mobile class is created and sent to superclass Phone

Let me if their is any mistake. */
Harsh Pathak
Greenhorn

Joined: May 09, 2006
Posts: 4
So u mean to say that the 'showDevice' method of child class is invoked in the constructor for the parent class; this device accesses the member of the child class; but that member is not initialized because the parent constructor invocation has not been completed; hence this member is null, and so null is printed?
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
Thats right
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
Yes! Thats what I meant.
gurpreet singh
Ranch Hand

Joined: Apr 07, 2006
Posts: 38
does that mean that becoz the runtime object was of subclass so the overriden method was called in superclass.

if we try to call to do like this:

Phone() {
showDevice();
System.out.println(device)
}
output is hone.device
why?


SCJP
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
does that mean that becoz the runtime object was of subclass so the overriden method was called in superclass.

if we try to call to do like this:

Phone() {
showDevice(); // line (1)
System.out.println(device) // line (2)
}
output is hone.device
why?

1)Yes runtime object is an instance of subclass(Mobile).
2)instance of subclass is created in the subclass and sent to super class constructor for initilization of super class fields with in subclass.
3)does that mean that becoz the runtime object was of subclass so the overriden method was called in superclass.-->Yes.

4)In line (1) showDevice method called will be of the subclass(Overriden showDevice method in subclass).
>>>Remember : Call to non-final instance methods will be mapped dynamically at runtime.Call depends on the runtime instance/object being pointed to.
5)In line (2) device field used will be of the superclass.
>>>Remember : fields accessed will always depend on the declared reference type of the reference variable.
6)Methods can be overriden where as attributes can not be overriden.
Ashok S Yadav
Greenhorn

Joined: May 04, 2006
Posts: 5
Harsh,
run the code below. This might help a bit but Girish's explain is very good. I wrote this piece of code and helped me understand the solution better.

public class Phone {

{
System.out.println("initialization of parent class");
}
static String device = "Phone.device";

public void showDevice(){
System.out.println("Inside parent showDevice");

}

public Phone(){
System.out.println("Invoking parent constructor()");
showDevice();
}
}

public class Mobile extends Phone {
{
System.out.println("initialization of child class");
}
String device = "Mobile.device";
public void showDevice(){
System.out.println("Inside child showDevice");
}

public Mobile(){
System.out.println("Invoking child constructor();");
showDevice();
}
public static void main(String[] args) {
Mobile n = new Mobile();
n.showDevice();

}

}

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding question
 
Similar Threads
method overriding
problem
Overloading,Overriding, Runtime Type
Class initialization order...
Constructor