File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Inheritance in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance in Java" Watch "Inheritance in Java" New topic
Author

Inheritance in Java

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
I want to know following:
I have three classes.
Parent.class
child1.class
child2.class
Public class Parent extends Applet{
public void init()
{
child1 c1 = new child1();
child2 c2 = new child2()
}
}
public class child1
{
public void send()
{
x = a+b;
}
}

public class child2
{
public void getValue()
{
QUESTION: I want to access the value of x?
How can i access without calling send()?
}
}
QUESTION: I want to access the value of x in child2.class,getValue() method without calling send() of child1.class?

And also if i import child1.class in child2.class. DID I have make reference to child1.class in child2.class?
Please let me know
Thanks in advance,
angela
Bruce Wingate
Ranch Hand

Joined: Feb 16, 2001
Posts: 32

I have three classes.
Parent.class
child1.class
child2.class

These classes are not parent and children based on your definitions below.

public class child1
{
public void send()
{
x = a+b;
}
}

public class child2
{
public void getValue()
{
QUESTION: I want to access the value of x?
How can i access without calling send()?
}
}
QUESTION: I want to access the value of x in child2.class,getValue() method without calling send() of child1.class?

x is a local variable to the send() method. After the method runs, it is "out of scope" and essentially disappears. You could declare x as a public variable in the class, then you could just reference it via by saying c1.x. *BUT* child2 would have to have a reference to the child1 variable.
And also if i import child1.class in child2.class. DID I have make reference to child1.class in child2.class?

As the program stands now, you don't need to import either one. If you were to reference child1 inside of child2, then you would have to import child1. If you don't reference child2 inside of child1, then you don't have to import it. (Someone correct me if I'm wrong, because I'm not positive on this.)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
They way you have it written now, x is a local variable to the send() method, and will not exist outside of it, even in the Child1 class. They ways to make it available include:
put a return type on the send() method and return x. Then Child 2 can make an instance of Child1 and call it's send method.

Or you can declare the x in the class (not the method), then you can access it directly

The last way is if x is a static variable of the class

[This message has been edited by Cindy Glass (edited February 23, 2001).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
 
subject: Inheritance in Java