• 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:

Question about Parent call childe method

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all
The followinf is the mock exam on http://www.geocities.com/sahirshah/applets/mocktest.html
I don't understand the result
Here is the code for question 1
........................
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
................................
Question 1 answer is 40
Could any one can tell me why the parent woulc call the child method rahter then it's own method ?
Question 2 is almost the same but method and variable changed to static
....................................
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
.................................................
The answer is 30
Anyone can help me ?
I am confuse about this
Thanks
 
Edwin Wong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for typing mistake..
The coding of question 2 should be
...........................
class Base{
static int value = 0;
Base(){
addValue();
}
static void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
static void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
....................
Thanks a lot
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Wong:
Dear all
The followinf is the mock exam on http://www.geocities.com/sahirshah/applets/mocktest.html
I don't understand the result
Here is the code for question 1
........................
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
................................
Question 1 answer is 40
Could any one can tell me why the parent woulc call the child method rahter then it's own method ?
Question 2 is almost the same but method and variable changed to static
....................................
class Base{
int value = 0;
Base(){
addValue();
}
void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
.................................................
The answer is 30
Anyone can help me ?
I am confuse about this
Thanks


Hello Edwin,
First, let's reafirm that 'static' means belonging to class only not to any instance / object. Non-static means that you need an object reference (i.e an instantiation is required).
Second, let's also reafirm that when you provide a non-static overriding method (i.e. method that has the same name and signature in both subclass and superclass), you are imposing a polymorphic operation. That means the object reference will dictate which method (child vs. parent) will be invoked.
Third, let's remember that within the default constructor (i.e the one without parameter) of the Derived class there is a call to its superclass's default constructor; In your case that would be super() or Base.
Now let's review your code. When you intstantiate a Derived object, first default Base constructor is invoked and that will call addvalue(). The question is which addvalue() is called (child's or parent's / Derived's or Base's). Now remember the 2nd reafirmation and ask yourself which object is instantiated. It is Derived, isn't it? even though you declared it to be class Base via an assignment. So Derived's addvalue() will be invoked. Thus 'value' now stores 20 (value += 20). After the Base's constructor then Derived's constructor will be called. And that will increment 'value' by another 20. Hence the answer is 40.
In the 'static' case, remember the 1st re-afirmation, the method declared with the access keyword 'static' belongs to the class. So when you instantiate the Derived class, again you first go thru. the Base's constructor; therefore you call Base's addvalue() and increment 'value' by 10 (value += 10). Then next when Derived's constructor is invoked, its constructor is called, which calls its addvalue() {value += 20), which increments 'value' by 20. Thus the answer is 30.
Hope that helps,
Lam

[This message has been edited by Lam Thai (edited April 16, 2001).]
 
Edwin Wong
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lam
In order to clear my confusion, I want to ask one more question
Is that mean Polymorphism(runtime binding) won't work when the method is static ?
Just want to have a more clear picture
Thanks
 
Lam Thai
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Edwin Wong:
Thanks Lam
In order to clear my confusion, I want to ask one more question
Is that mean Polymorphism(runtime binding) won't work when the method is static ?
Just want to have a more clear picture
Thanks


Hello Edwin,
First, the rule is that you CANNOT have a 'static' method in a subclass overrides the parent's non-static and vice versa. The compiler will not like that at all.
Now regarding your question - Yes, run-time binding does not apply when the methods, in both subclass and superclass, are declared static. Try to compile and run the following program:
class Animal {
void sound() {
System.out.println("Sound of an animal");
}
}
class Dog extends Animal{
void sound() {
System.out.println ("Wooff");
}
}
class Cat extends Animal{
void sound() {
System.out.println ("Meow...");
}
}
public class Test {
public static void main(String[] agrs) {
// instantiate Dog and Cat then assign them to
// Array of Animal called Pets
Dog MyDog = new Dog();
Cat Mycat = new Cat();
Animal[] Pets = { MyDog, MyCat };
Animal PetChoice;
for (int i = 0; i < Pets.length; i++) {
PetChoice = Pets[i];
PetChoice.sound(); // polymorphic if with public sound()'s
}
MyDog.sound();
Mycat.sound();
}
}
The program will print:
"Wooff"
"Meow..."
"Wooff"
"Meow..."
As you can see, PetChoice, an Animal class can 'sound()' on the behalf of Dog and Cat.
Now if you declare all void sound()'s with 'static', you will see that the answer will be:
"Sound of an animal"
"Sound of an Animal"
"Wooff"
"Meow..."
See the difference? The answered strings are dictated by the classes not by the object references. Remember 'STATIC' goes with class, NON-STATIC goes with object reference (i.e required instantiation)
Now replace class Test above with the following:
public class Test {
public static void main(String[] agrs) {
Dog.sound();
Cat.sound();
Animal.sound();
}
}
Compile and run the program again, what do you see? If the methods are all declared with 'static', you don't need their objects to be able to invoke their methods; All you need is to prefix the methods with the class names!
Does this help answer your question?
Take care,
Lam

[This message has been edited by Lam Thai (edited April 16, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic