• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Inheritance in Java

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.)
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic