aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inherited methods works on hidden instance variable while .... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inherited methods works on hidden instance variable while ...." Watch "Inherited methods works on hidden instance variable while ...." New topic
Author

Inherited methods works on hidden instance variable while ....

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Source : built myself...

I cleared one doubt during my scjp practice.
Inherited methods works on hidden instance variable.
while overriden methods works on hiding instance variable.
for ex:


here output is java, that is set in SuperClass constructor.
But if I override getName() function in DerivedClass.


then output comes name is : ranch

Please correct me, if I am wrong....

[ December 07, 2008: Message edited by: punit singh ]

[ December 08, 2008: Message edited by: Punit Singh ]
[ December 09, 2008: Message edited by: Punit Singh ]

SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Originally posted by punit singh:
Inherited methods works on inherited/hidden instance variable.
while overriden methods works on overriden instance variable.


I don't understand what you are saying here. Lets take an example



Now the output will be
Super.name=ankit
Sub.name=null

So basically fields access is never polymorphic. If you are trying to say something else, then please state it more clearly...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
why the output sub.name=null comes here in your code?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

why the output sub.name=null comes here in your code?


That's because Sub class hides the inherited name from the Super class. If you comment the line " String name;" in Sub class it'll print the same value for both.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Punit,

I miss to get what exactly you want to convey.

You don't find polymorphic behavior with Instance variables.
In your example, if thinking that through inherited method getName(),
you can access the duplicated variable name of the subclass, that is not true.

When you override the getName() method, then its not going to read the
instance variable of the super class because its there in subclass also,
so value of subclass name variable gets printed there.

You can understand in a way as:

- Instance variables can be inherited so that you can reuse the stuffs
the parent class is already having, but there is no polymorphic behavior.
Duplicating the name of the instance variables in subclass causes confusion
and its termed not a good practice.


cmbhatt
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
if you override setName() in sub class, output will change.



super.name=null;
sub.name="punit."

I want to say when we hide instance variable name in subclass name , only overriden method can modify this subclass name instance variable, no superclass method can modify the subclass name. As in your case.
I want to say this thing may be I am not able to say this in right way.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


super.name=null;
sub.name="punit."




Because setName() of the superclass is never invoked. And its not the
constructor that would have got called before initializing the subclass
object by executing the subclass constructor code.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Originally posted by Punit Singh:
I want to say when we hide instance variable name in subclass name , only overriden method can modify this subclass name instance variable, no superclass method can modify the subclass name.


Yes you are right. Your language confused me. Take this example

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ya good example.
This type of question confuses mostly in mocks,
so I raised this question for discussion.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Actually, good question Punit.Even I get confused in questions like these.I thought the answer would have been ranch.
Shwetha Sharma
Ranch Hand

Joined: Jun 01, 2008
Posts: 34

The example given by Ankit was explainative but i still have a doubt in inherited methods.

When we extend a class, the methods of the super class( depending on the access modifier) are inherited in the sub class.

In this case the method getName gets inherited in the sub class and can be used as if it was a method of sub class itself (this is what i understood about inheritance)
I added some SOP's in Punit's program :
class BaseClass {
public String Name;
public BaseClass(){
this.Name="java";
}
public String getName(){
System.out.println("Base Class Method called on object of :: "+this);
System.out.println("Base Class Method :: "+this.Name);
return this.Name;
}
}

class DerivedClass extends BaseClass{
public String Name;
public DerivedClass(){

this.Name="ranch";
}
}
public class baseVar{
public static void main(String[] args) {
DerivedClass dc=new DerivedClass();
System.out.println("Object of Derived Class :: "+dc);
System.out.println("dc.Name :: "+dc.Name);
System.out.println("name is: "+dc.getName());
}
}



This gave the output as:
Object of Derived Class :: DerivedClass@923e30
dc.Name :: ranch
Base Class Method called on object of :: DerivedClass@923e30
Base Class Method :: java
name is: java


My question is when in getName() method this is an Derived Class object then why it returns java instead of ranch


SCJP6:80%, OCWCD 88 %
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
My question is when in getName() method this is an Derived Class object then why it returns java instead of ranch


Derived class has two String name;
one that is inherited from superclass. It is hidden. call it super.name
second itself declared in derived class. call it sub.name

Derived class has inherited method getName(), but this method is actually
defined in superclass not in derived class. so this method cannot see
sub.name that is declared in sub class. It can only see super.name variable.

if you override getName() in subclass then it can see sub.name.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inherited methods works on hidden instance variable while ....