• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

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

 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I don't think there is.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Thanks...i understand wrongly...
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic