• 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

inheritance

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I access the superclass method using the object of a subclass.
eg:
class a
{
public void add()
{
System.out.println("hello");
}
}
class b extends a
{
public void add()
{
System.out.println("hello1");
}
public static void main(String arg[])
{
a a1=new a();
b b1=new b();
thru b1 i want to print hello.coz when i call b1.add(),method overriding will take place.Plz help.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS 8.4.6.1 Overriding (by Instance Methods)


An overridden method can be accessed by using a method invocation expression (�15.12) that contains the keyword super. Note that a qualified name or a cast to a superclass type is not effective in attempting to access an overridden method; in this respect, overriding of methods differs from hiding of fields. See �15.12.4.9 for discussion and examples of this point.



Also have a look at the example shown in JLS 15.12.4.9 Example: Method Invocation using super
Let us know if this is still not clear...
[ June 13, 2002: Message edited by: Valentin Crettaz ]
 
clyde mel
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that the code super.add() is one of the options.But without doing any modification in the code is it possible to print hello thru the object of subclass.State reasons.Thanx
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AFAIK, there's no way to do what you want to do. Why would you want to do that anyway? I can't think of any real-world application for that capability that keeps with the OO way of thinking.
 
crispy bacon. crispy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic