• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hi a simple query

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following code
class B {
int i=100;
void amethod1() {
System.out.println("I am in B's amethod");
}
class C extends B {
int i=200;
void amethod1() {
System.out.println("I am in C's amethod");
}
public static void main(String args[]) {
B b=new C();
System.out.println(b.i);
b.amethod();
}
}
when the programmed is compiled and run it gives me an o/p of
100 and i am in C's amethod
further if i modify the code a bit and include another method called amethod2() in class B and donot override it in C
and call it from class C it shows that I am in B;s amethod 2 fine
but when i do override it then it starts showing I am in C's amethod2
my question when we refer to a variable present in both theclass it will pick the value thats in parent class as b.i=100
but when we call a method it picks the method from the current class
but when we add another method and do not override it executes the code of the method thats in parent class why is this happening
can anybody expalin it to me
regards
amit

------------------
coffe drinker
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe i don't understand your question.... but here

"when we add another method and do not override it executes the code of the method thats in parent class why is this happening"
The subclass inherited that method and since subclass didn't
override it this method got called.
Subclass inherits all the members of supclass so it has access to all of them depends on the modifier of the members.
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fei i just need to know since we are writing
B b=new C() we are creating an object of C
but when we call a method of B which we override it in class C
it picks up the method thats present in C but when a value such as var i in above class and which is present also in Class C
we see that it picks up the value of variable which is present in class B and not the value which is present in class C
why?
hope that this makes my question more clear
------------------
coffe drinker
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit
What your asking about is the difference between shadowing and overridding. See if this helps:
Shadowing
applies to instance variables as well as class methods and class variables. In shadowing the value (method call if class method) is based on the type of the variable not the type of the object refered to by the variable. Shadowed variables accessed from an inherited method will reflect the value from the class the method is inherited from. The following code prints 'i in Test:1':

Overridding
applies to methods that are overridden in a subclass. An overridden method is looked up through late binding and is determined by the type of the underlying object refered to by the variable, not by the type of the variable.
Hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement: B b = new C() creates a new C object and casts it as a B. When referencing shadowed variables, the one that is chosen is the one specified by the cast. In this case, B.
When referencing overridden methods, the method run will be the one that is farthest down the override chain, but still within the created object. In this case, C (remember, we created a new C in the declaration).
 
amit mawkin
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks this clears my doubt but can u suggest more reading for me on this topic
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit mawkin:
thanks this clears my doubt but can u suggest more reading for me on this topic


Hi amit,
They explained very clearly. I am too late, heheheheheee.
a good link http://www.janeg.ca/scjp/oper/cast.html
take a look.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic