Win a copy of Terraform in Action this week in the Cloud forum!
    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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Help on overiding

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator



why we got Output like this :

class A has name A
class B has name A

[ August 05, 2007: Message edited by: Mike Anakin ]
[ August 08, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I see that public String getName() {return name;} is defined in class A. The variable name is defined as "Class A" in class A. Although class B inherits the function, that function still looks at the variable name of class A, not the instance variable of class B. And that is because it is a variable.

One thing you have to remember is that only public and protected instance methods are used/looked at when you execute a statement that seems to have something to do with overriding. Hence instance variables, static functions and private functions do not play a part in this game. Hence if you call getName from B, it executes the A getName and gets the A name instance variable.

I hope this helps.

Marc
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Just adding to what marc said
getString() is ovveriden and getGreeting() is not ovverriden

A a = new A();
B b = new B();
a.greeting() will invoke the greeting() that is defined in the object pointed by reference a which is class A object and hence a.getgreeting() will invoke greeting() of class A

Same applies to a.getName()

With same reasoning as above b.greeting() will invoke the greeting() that is defined in class B, But class B does not have greeting() or does it have, Since class B extends A , greeting() is inherited by class B but since it doesnt override it , greeting() of class A is invoked. However if greeting() were to be declared private in class A then class B would not have inherited it and b.greeting() will result in a compilation error.

b.getName() This will invoke the overriden method in class B because class B overrides it and reference "b" refers to class B object at runtime.

Overriden is defined only for instance methods.

Just for information

Here
Here any access to varaibles of class PArent or class Child are resolved at compile time and not at runtime as in case of instance methods. Hence
Hence p.parent in System.out.println("p.parent:"+p.parent); will display the value of parent in class Parent, which is expected.

And c.parent in System.out.println("c.parent:"+c.parent);
will display the value of parent in class Child, which is expected.

But if i assign parent class reference to child object [polymorphism]
"p = c;" then System.out.println("p.parent:"+p.parent); will again refer
to the parent value in Parent class and not the one defined in Child class. This is because the access to variables is determined at compile time.
Hope this helps
Thanks
Deepak
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Deepak Jain:
With same reasoning as above b.greeting() will invoke the greeting() that is defined in class B, But class B does not have greeting() or does it have, Since class B extends A , greeting() is inherited by class B but since it doesnt override it , greeting() of class A is invoked. However if greeting() were to be declared private in class A then class B would not have inherited it and b.greeting() will result in a compilation error.

b.getName() This will invoke the overriden method in class B because class B overrides it and reference "b" refers to class B object at runtime.

You have confused the methods. greeting() is overwritten and getName() is not overwritten.
 
Mike Anakin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
to Deepak Jain

I think you have got a confuse about the methods.

would you read my code again .

thank you all the same
 
Mike Anakin
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
nobody knows why ?

can someone explain ?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Mike,
I understand what you are trying to point out.There should be a compile time error "Variable already declared in class"(Inherits from the super class).

It makes sense when we override a method (redefine the action).But it doesn't make sense to redeclare a variable or place holder( we can re-assign a value).


But as per the compiler specifications,

This is something to do with the Scope of Variables and Members,

#1 If you are accessing the member using the reference of the parent, then Parent class member is invoked. (always look for nearest members, if not found then broaden the scope)

#2 If you are accessing the member using the reference of the Child, then the look for the Child Class first, if not available then for the parent class.

Moral of the Story : Always look for nearest members, if not found then broaden the scope.
 
    Bookmark Topic Watch Topic
  • New Topic