• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I call a class' superclass' method from outside the class?

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, coming from c++. Is there any way to call a superclass method of an object from outside the object?

 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I don't think there is.
 
Willy Ray
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about this?

class Base
{
public String get()
{
return "base";
}
}

class Derived extends Base
{
public String get()
{
return "derived";
}

public String getFromSuper()
{
return super.get();
}

}

public class test
{
public static void main(String[] args)
{
Base b = new Derived();

//Call the base method something like this
//b.super.get();
b.getFromSuper();
}
}


Does this work?

/w
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

public class test{
public static void main(String[] args){
Base b = new Derived();
//Call the base method something like this
b.super.get(); }
}

-------------------------------------------
Base b = new Base(); insteadof Base b = new Derived();
----------------------------------------

thanks®ards,
seetharaman
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Willy Ray.

You need to declare b as type Derived; there is no getFromSuper method in Base. Then your code will work, but you are not calling super from outside the class; it is inside the subclass. Please use the code button below the message window whenever you quote code; it preserves the formatting and makes it easier to read.

Seetharam, I am afraid your solution won't compile.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

you mean to say it won't compile!!!

------------------------------------------------------
class Base{
public String get(){
return "base";
}}

class Derived extends Base{
public String get(){
return "derived";
}}

public class Merrill{
public static void main(String[] args) {
----------------------------
Base b = new Base();
-------------------------------
//Call the base method something like this
System.out.println(b.get());
}

}

----------------------------------------------------------

thanks & regards,
seetharaman
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I mean to say it won't compile.

The original question is, can you declare an object and then put .super after its identifier. Answer, as far as I can tell: No.

I tried it, following your suggestion, and it won't compile.

There have been two suggestions in this thread which do compile and run, neither using b.super.
 
Rameshwar Kharat
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe we can call the super() withing class to refer the Base class.
We can't use the object.super().
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks...i understand wrongly...
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic