1

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.

9
  • What makes this a bad question? Commented Apr 17, 2014 at 4:53
  • I don't think this is a bad question. The question is on topic and clear to understand. I've upvoted it to compensate for the negative vote somebody made. Commented Apr 17, 2014 at 4:55
  • Not a downvoter - but that completely defeats the purpose of polymorphism. You can call the super.method1() in B. Commented Apr 17, 2014 at 4:55
  • @ElliottFrisch I receive the object as an A, so I can't call a.anotherMethodInB() because I don't know that it is a B. Commented Apr 17, 2014 at 4:59
  • 1
    You also need to look up the difference between method overload and override. What you meant in this question is method override. Commented Apr 17, 2014 at 5:00

4 Answers 4

3

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.

i.e., 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.

Sign up to request clarification or add additional context in comments.

7 Comments

Okay, so there is no way to reference an object's superclass from outside of the object itself, at all.
@David There is no way of using outside of class.
@David: not that I know of, but this breaks all sorts of OOP rules. It carries a terrible code smell to it. What is the need for this??
@HovercraftFullOfEels I am writing a class for an assignment that has 3 account types that are all subclasses of a parent Account abstract. I need to be able to cancel transactions and the easiest way to do that would be to call the superclass's methods, as they do not have any fees or other variable changes associated with the specific account type. Additionally, in the method, I receive them as Account objects, rather than as the specific accountType objects.
@David: each class should have its own cancel method, one that should handle the specifics of that class. Since the cancel method is within the class, then inside of cancel, of course you can call super methods.
|
0

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()
 }
}

Comments

0

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.

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.