aspose file tools*
The moose likes Beginning Java and the fly likes object references Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object references" Watch "object references" New topic
Author

object references

Alex Black
Greenhorn

Joined: Apr 28, 2001
Posts: 16
Hello, I can't seem to understand object references.
For example, if I have a superclass A and a subclass B. What
does this line of code denote.
A a = new A();
A a = new B();
Like at (1), why is it not possible to
call sup.hello?
Thank you very much for your time
public class PrivateTest {
public static void main(String s[]){
new PTSuper().hi();
new PTSub().hi();

PTSuper sup;
sup = new PTSub();
sup.hi();
sup.hello(); //(1) Why can't I do this?
}
}
class PTSuper {
public void hi() {
hello();
}
private void hello() {
System.out.println("hello-Super");
}
}
class PTSub extends PTSuper {
public void hi() {
try {
hello();
}
catch(Exception e) {}
}
void hello() throws Exception {

System.out.println("hello-Sub");
}
}
Thiru maran
Greenhorn

Joined: Apr 25, 2001
Posts: 4
Hai black,
Its simple. When you are trying to overide the method hello which does not have similar signatures inthe base class.Only the methods with same signatures can be overriden.
I think i have answered your question.
Maran.
Jyotsna Clarkin
Ranch Hand

Joined: Jan 26, 2001
Posts: 158
Hi Alexander,
You have a couple of different queries so I'll answer them separately.
First - objects and object references
I have a superclass A and a subclass B. So let's look at the hierarchy (simple)
<pre>
class A
|
|
class B (extends A)
<pre>



Hope this anwers the first part of your question.
regards,
Jyotsna


[This message has been edited by Jyotsna Clarkin (edited May 03, 2001).]
rajesh purohit
Ranch Hand

Joined: May 02, 2001
Posts: 33
I hope previous answer will solve ur first problem..
for other problem.
u had written
private void hello() {
this is a private function and can not be accessible from outside the class. it is available inside the supar class only and even not to the subclass.
Private members are available to the class only.
public are available to all the methods and member obj. even outside the class
U can use <code> protected</code> if u want ur method to use in subclass.
if u have prob. then feel free to contact me
Rajesh Purohit
India
Originally posted by Alexander Black:
Hello, I can't seem to understand object references.
For example, if I have a superclass A and a subclass B. What
does this line of code denote.
A a = new A();
A a = new B();
Like at (1), why is it not possible to
call sup.hello?
Thank you very much for your time
public class PrivateTest {
public static void main(String s[]){
new PTSuper().hi();
new PTSub().hi();

PTSuper sup;
sup = new PTSub();
sup.hi();
sup.hello(); //(1) Why can't I do this?
}
}
class PTSuper {
public void hi() {
hello();
}
private void hello() {
System.out.println("hello-Super");
}
}
class PTSub extends PTSuper {
public void hi() {
try {
hello();
}
catch(Exception e) {}
}
void hello() throws Exception {

System.out.println("hello-Sub");
}
}


Bye for Now<P>Rajesh Purohit<BR>INDIA
Alex Black
Greenhorn

Joined: Apr 28, 2001
Posts: 16
That pretty much ends my confusion with object references although I am still not to clear with the overriding bit.
Thanks again
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: object references