Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object references

 
Alex Black
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
}
}

 
Alex Black
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That pretty much ends my confusion with object references although I am still not to clear with the overriding bit.
Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic