• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Constructor question

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am BaseClass");
}
}

public class SubClass extends BaseClass{
static void sayHello(){
System.out.println("Hi pal!!!, I am SubClass");
}

public static void main(String [] arg){
BaseClass bc = new SubClass();
bc.sayHello();
}
}
when i compile and run i am getting
Hi pal!!!, I am BaseClass
My understanding is that even though bc is reference of BaseClass but the object created is that of SubClass() so the SubClass constructor should be called.
Please advice
Thanks in advance
kareem
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are making a call to a static method. Static methods are not called polymorphically. That is, at runtime, the program doesn't care what the actual object is. It only cares about the type of the reference being used to make the method call. The method of that class will be called, regardless of the actual class of the object.
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob,
Yes when i removed the static from both the classes and then when i compile & run, i get what i was expecting "..I am SubClass...."
Please explain in detail
my understanding is the reference of BaseClass bc lies on the stack and the static method sayHello is also created on the stack.
Now the new object of SubClass is created on the heap and it reference is bc, also it static method sayHello is created on stack. Since bc is the reference of the object SubClass which ever static method is called for SubClass if similar method is contained in BaseClass then that method will be called else method in SubClass is called.
Is my understanding correct?
Thanks
Kareem
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If my understanding is correct the following are true:
References to member variables are computed at compile time using the type of the reference.
References to member methods are resolved at runtime using the type of the object.
References to class methods are computed at compile time using the type of the reference.
Using this same type of wording, how would you state references to class variables?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
References to class variables are computed at compile time using the type of the reference.
In fact, you can simplify your memorization when you realize that ONLY non-final instance methods in non-final classes are eligible for run-time binding/polymorphism. All other types of reference invokations are staticly bound, ie, made at compile time.
 
Alvin York
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will certainly be grateful when I reach a comfortable level of understanding of the Java language. I've been studying Java for 2 1/2 months now, and sometimes I feel that my head is about to explode trying to grasp how the heck everything works. Thanks to all of you for your patience and compassion when answering questions that must at times seem to you to be completely idiotic.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alvin I hear ya. My exam is scheduled for Monday morning. My brain has already exploded a couple of times. I took a class as an intro to java in September. I've been preparing for the certifcation exam since January. I don't know how much more my brain can hold.... and still I'm not sure if I know enough to get 36 questions right on a 59 question exam.
 
Alvin York
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Best of luck Paul. If you get the chance email me at AlvinYork@hotmail.com and let me know how you did.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good luck Paul.
You should give your brain a rest sometimes. You have to give it the chance to digest what you feed it. Otherwise it's going to throw up
Out of experience, overstudying is not the key to learning faster. In fact, exactly the opposite may happen.
Wish you all the best
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic