简体   繁体   中英

Java inheritance call object's superclass's method

I have a class A:

A
- void method1

And a class B that extends A that overrides A's method1:

B->A
- void method1

Later on, I create an instance of B that is referenced as an A:

A a = new B();

I want to use A's version of method1 in a specific case. Is there a way to call this, like:

a.super.method1();

Edit:

To clear up any confusion, here is what I am trying to accomplish:

I have a parent class called Account. Beneath that I have 3 subclasses, SavingsAccount, CheckingAccount, and MoneyMarketAccount.

Each of these has its own deposit/withdraw methods that have specific fees associated to them.

There is a manager class called Bank that has an ArrayList of Account objects in it. I have a method, void transfer that I am attempting to create that will allow me to take the two accounts and transfer the money between them.

Sometimes the different methods will throw exceptions if there are insufficient funds or other errors, so I need to be able to reverse a transaction if it fails for one of the two accounts.

This is where I am having problems:

I need to redeposit the money withdrawn from the from-account if the to-account can't receive deposits, but the to-account may have deposit fees, so I would like to just call the parent Account class's deposit method, as it does not have any fees associated; however, from the comments and answers that I have received, I may have to just write another method that is not overridden that simply performs the same operation as Account's deposit.

You can't use super directly since it only exists inside of B. But you can do it indirectly:

public B extends A {
  public void method1() {
    // B's method 1
  }

  public void superMethod1() {
     super.method1();
  }
}

So if a class uses a B variable, then they can call the superMethod1() and get A's method1.

But having said this, this won't work on A variables, as your code is currently set up, not without casting.

ie, you could do:

B b = new B();
b.superMethod1();

But this won't work for your set up:

A a = new B();
// not without casting
((B)a).superMethod(); // ugh... ugly! AND dangerous!

Note that your question, while itself not bad, suggests possibly a bad program design and code needing this has at least for me, a bad code smell.

You can have super or this keywords only with in the class. You cannot use them outside of that class.

So with in the class you can do (Mock code)

class A {    
  someMethod(){    
  super. someMethod()
 }
}

You can't do that from the client code, ie. the code calling the method. You can only do it within the sub class method

super.method1();

and only for one level up the hierarchy.

use A a = new A(); or you can call super.method1() from B's method1 if you want to go like A a = new B();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM