Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why the output sub.name=null comes here in your code?
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya good example.
This type of question confuses mostly in mocks,
so I raised this question for discussion.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, good question Punit.Even I get confused in questions like these.I thought the answer would have been ranch.
 
Shwetha Sharma
Ranch Hand
Posts: 34
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic