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 inheritance 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 "inheritance" Watch "inheritance" New topic
Author

inheritance

cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Dear all,
I ask myself about some of the rules governing the choice of variables or methods in inheritance.
1�) Is it correct to say that the same rules govern the choice of instance variables and static methods during process of subclassing?
I mean the choice of both of them depends on the type of the reference at compile time?
And there's another rule governing the choice of instance method, telling that this must be chosen depending on the type of the object referenced at runtime
So, the following code:

implies the following output:
B.s1 A.s2 A.s1 A.s2
B.m()
A.m()
x.s1 and x.m() call the instance variable and static method of B, whereas y.s1 and y.m() refere to to the instance variable and static method of A. It is the same logic in both cases.
Am I right here?
2�)question extracted from Dan's exam:

Response: H.printS1,null
Explanations:

The default constructor of H calls the constructor of G. The constructor of G calls the overridden method printS1 causing the invocation of H.printS1. The instance variable H.s1 hides G.s1. Although G.s1 has already been initialized, the initialization of the instance variables of H won't begin until the initialization and construction of G is complete.

I understand H.printS1, but definitly not the null.
Could someone give me light about this?
Thanks in advance for your response,
Cyril.


SCJP 1.4, SCWCD, SCBCD, IBM XML, IBM Websphere 285, IBM Websphere 287
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

As far as your first question, you are correct (assuming that I've read it right).
The second question breaks down into a matter of timelines:
Request new H() (Zeroes memory so have blank object)
- Call default constructor H()
--- call super constuctor G()
--- initialize G.s1 = "G.s1"
--- call this.printS1()
----- through polymorphism, method goes to H class's printS1().
----- H class's s1 has not yet been initialized, so h.s1 = null. Print (H.prints1,null);
----- return from method
--- return from super constructor.
- initialize h.s1 = "H.s1"
- return from default constructor
Remember, the call to the super class's constructor occurs before anything else in the constructor--even the initialization of the default variables.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
thanks Joel for your very good and clear explanations.
I've a
Cyril.
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
... good grasp of this topic now.
Thanks a lot.
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247

Is it correct to say that the same rules govern the choice of instance variables and static methods during process of subclassing?
I mean the choice of both of them depends on the type of the reference at compile time?


I could have added to this rule the case of overloaded methods, that are chosen based on the referenced type too.
Example extracted from Dan'x mock exam:

Output: ABC because reference c4 refers to an object of type C that has three overloaded implementations of method m1.
But just changin the referenced type of c4 as follows:

gives AAA as output. Because c4 now just has access to A.m1() method.
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
Hi cyril,
This thread was very informative. But I think there is some typo in the last example you gave. Class D doesn't has m1 method. There is a compile time error in this program.
Could you please let use know the correction.
Thanks
Lalitha
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
There are a lot of errors in the second program posted by Cyril.
Darren McLeod
Greenhorn

Joined: Jul 07, 2003
Posts: 19
change
D c4 = new C();
to:
A c4 = new C();
and you will get the AAA result he describes. Cyril posted another thread describing this effect.
[ July 17, 2003: Message edited by: Darren McLeod ]

SCJP, SCBCD
Lalitha Chandran
Ranch Hand

Joined: Jul 03, 2003
Posts: 92
Thanks Darren...
cyril vidal
Ranch Hand

Joined: Jul 02, 2003
Posts: 247
Oh, I'm sorry, I did'nt connect for a long time and couldn't answer to the question of Lalitha.
Of course, Darren is right.
You juste have to change the type of c4:
A c4 = new C();
and you get the correct output: AAA
I'm sorry for this mistake,
Cyril.
 
jQuery in Action, 2nd edition
 
subject: inheritance