• 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
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

hiding/Overriding

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone explain to me the diff between hiding and overriding of methods/fields with some examples..
-Sanjana
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you extend any base class, all the members of base class will get part of the derived class. Based on their access modifiers these memebers get accessed from the new class.
While defining the derived class if you try to change the behaviour of a particular method, with which the signature is already inherited from base class, we call it as overriding. Similarly, if you define any member variable with the same name in derived class we call it as hiding.
In both the cases we are redefining the inherited members. The only difference between these is when you access them. JVM decides which overridden method to call based on the object referrece that curently pointing to. Incase of member fields it's the Class type of the reference it's pointing. Try to run the below program ...
import java.io.*;
class A
{
String str="This String is defined in A";
public void printMsg()
{
System.out.println("This method is defined in Class A");
}
}
public class B extends A
{
String str="This String is defined in B";
public void printMsg()
{
System.out.println("This method is defined in Class B");
}
public static void main(String[] a)
{
B testB=new B();
A testA=testB;
System.out.println(testB.str);//This prints B String
testB.printMsg(); //This prints B Message
System.out.println(testA.str);//But, this prints A String
testA.printMsg(); //This prints B Message
}
}
 
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


While defining the derived class if you try to change the behaviour of a particular method, with which the signature is already inherited from base class, we call it as overriding.


This is not applicable to static methods. Static methods cannot be overridden.
[ December 07, 2003: Message edited by: dennis zined ]
 
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose class A has a static method show and class B which extends A has redefined the same method then we say that show in B has hidden show in A.
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Suppose class A has a static method show and class B which extends A has redefined the same method then we say that show in B has hidden show in A.


Exactly. Its important to understand that hiding is different from overridding. Let say for example you assign a reference variable with its subclass type:
<Superclass> A = new <Subclass>;
and a "static" method show() in the Superclass has been hidden in its subclass so that both the Superclass and subclass now has the static method show(). When you access the static method show() via the reference variable: A.show() the question is which show() method is accessed? The answer is the static method show() that is defined in the Superclass. But if the show() method was not declared as static but rather as an instance method, the show() method called is the one that is defined in the subclass.
So Sripada's statement:


JVM decides which overridden method to call based on the object referrece that curently pointing to.


is only applicable to instance methods and not static methods and variables.
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u guys..

-Sanjana
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic