aspose file tools*
The moose likes Beginning Java and the fly likes Can I call a class' superclass' method from outside the class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can I call a class Watch "Can I call a class New topic
Author

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

Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
Sorry, coming from c++. Is there any way to call a superclass method of an object from outside the object?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
No, I don't think there is.
Willy Ray
Greenhorn

Joined: May 04, 2008
Posts: 11
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Mar 17, 2008
Posts: 3
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

Joined: Jan 28, 2008
Posts: 5575

Hi Campbell,

Thanks...i understand wrongly...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can I call a class' superclass' method from outside the class?