• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

inheritance

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Joel for your very good and clear explanations.
I've a
Cyril.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... good grasp of this topic now.
Thanks a lot.
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a lot of errors in the second program posted by Cyril.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Lalitha Chandran
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Darren...
 
cyril vidal
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic