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

Reg : doubt

 
ramkumar chennai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
 
ramkumar chennai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
ramkumar chennai
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here we not assign parent class reference to child class. It will not compile.
 
Neha Kathuriaa
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic