permaculture playing cards*
The moose likes Beginning Java and the fly likes Reg : doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reg : doubt" Watch "Reg : doubt" New topic
Author

Reg : doubt

ramkumar chennai
Greenhorn

Joined: Sep 08, 2008
Posts: 16
Hai All,

I have one doubt in following program.

class One
{
int ivar=5;
}

public class two extends one
{
public int ivar=10;

public static void main(String a[])
{
// Read Parent class variable (ivar)
}

}


Parent class variable and sub class variable has same name as "ivar".
How can if get value of parent class in child class?

Thanks and Regards,
Ram kumar
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ ramkumar

I think your question is not clear. Make sure that ranchers spend time in trying to find a solution to your question rather than try to understand your question


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
ramkumar chennai
Greenhorn

Joined: Sep 08, 2008
Posts: 16
Question :

Parent class and child class has same variable name. In child class how can read parent class variable.




Variable ivar has both classes in child class how can read parent class variable (ivar).
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

ramkumar chennai
Greenhorn

Joined: Sep 08, 2008
Posts: 16
Hai James ,

Is any other way, because we are extends parent class in child class.With out creating a parent class object in child class is any other way to read the parent class variable.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

To read a variable using a object... where there is only two class.
There are four way of creating a object.
Here they are.
1. This is the simplest way.
2. parent reference ,child object. you can access parent class ivar
3. This again simple. But cannot access Parent ivar
4. Compiles but does not run,because the compiler checks only one thing that is if they both are of the same inheritance tree.




Parent p= new Parent(); // one
System.out.println(p.ivar); */

Parent p= new child(); // two
System.out.println(p.ivar);

child c= new child(); // three
System.out.println(c.ivar);

child c= new Parent(); // Four , compiles but does not run.
System.out.println(c.ivar);
ramkumar chennai
Greenhorn

Joined: Sep 08, 2008
Posts: 16


Here we not assign parent class reference to child class. It will not compile.
Neha Kathuriaa
Greenhorn

Joined: Sep 16, 2008
Posts: 25


i am afraid how come the fourth one in the previous post getting compiled also.it is a compilation error..See here child is extending parent that means child is a parent not vice versa .
So according to me 4 th should give the compilation error(Cannot convert from Parent to chld).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Nobody mentioned the super keyword yet?

And, ramkumar chennai, pleas use the code button; you can see how much easier your code will be to read.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You are incorrect calling the first ivar "parent class variable."

"Parent" is not the approved Java term; you ought to say superclass.

It is not a "class" variable; it belongs to the instance (object). If you are writing object-oriented programming you ought to try to gain access from an object, not from the main method which isn't part of an object.

"Variable" is correct; "field" is better.

****************************************************************

So it ought to read something like "superclass instance field." When you read it like that, I hope you will take a hint as to how you can gain access to a "superclass instance field" from a "subclass instance method."

And I agree with James Tharakan that your thread title ought to match the thread contents.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reg : doubt