• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

overriding & instance variable

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may be a foolish question, but i am suddenly confused with the instance variables & overriding(rather hiding) them in subclasses of any class.....
while accessing hidden/overridden instance variables the accessed depends on the object's reference type only?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by B Misra:
This may be a foolish question, but i am suddenly confused with the instance variables & overriding(rather hiding) them in subclasses of any class.....
while accessing hidden/overridden instance variables the accessed depends on the object's reference type only?

 
Mack Stevens
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing that is important is the object ,not the reference type.Even if you are creating sub-class object using super class referenc variable,the sub-class copy is called.
 
B Misra
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the reply but thats not what i asked...

for example:

class Super{
public int i =5;
}

class Sub extends Super{
public int i=6;
}

like that.... the reference type of each type of object will determine which version of i is accessed?
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variable values is assigned at compile time, while variable method is invoked at run time, maybe!
am i right?


The Output is:
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Method Overriding to decide whether the super class method will be invoked or sub-class method will be invoked...you have to look at the object that the reference variable,which is used to invoke that method, is referring to at the run time..

While in case of instance/static variable we have to look at the class type of the reference variable to decide whether super class variable or sub-class variable will be used..

Example::


public class Test0901 {
public static void main(String[] args) {
Super su = new Sub();
System.out.println(su.i); // line 1
su.getI(); // line 2
}
}

class Super {
public int i = 5;
public void getI(){
System.out.println("Super: " + i);
}
}

class Sub extends Super {
public int i = 6;
public void getI(){
System.out.println("Sub: " + i);
}
}

O/P

5
Sub: 6



At line 1 the class type of reference variable su is Super, hence Super class i gets invoked...irrespective of what type of object it is referring to at run time...

While at line 2 the we look at the object that su is referring to at run time..and that object is of type Sub..hence Sub class getI() method is invoked..
[ September 01, 2007: Message edited by: Priyam Srivastava ]
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't get you what you are saying.I need more clarifications.Pls explain
regards
rex
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. see this code:

package chap_1;

class Test3 {

/**
* @param args
*/
public static void main(String[] args) {
sper su=new sper();
System.out.println(su.i);
su=new Sub();
System.out.println(su.i);
//=====
su=new sper();
System.out.println(su.getI());
su=new Sub();
System.out.println(su.getI());

}
}
class sper{
public int i =5;

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}
}

class Sub extends sper{
public int i=6;

public int getI() {
return i;
}

public void setI(int i) {
this.i = i;
}
}


Result:
5
5
5
6
=====
Here I am using a suprclass and assigning it a subclass. However when I use the getter method call(getI() is defiend n both the classes sper and Sub),see that the last 2 results 5,6 get printer based on the actual object and not the superclass reference. However the first 2 results 5,5 get printed when I access the variable directly using the reference variable of superclass. What this boils down to is that overriding at runtime works with Instance methods only. For variables that are redefines/hidden in a subclass will not be decided at runtime, but rather compile time.

Hope this makes sense.
 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Ranchers It cleared my doubt also.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic