Java Inheritance - calling superclass method
You can do:
super.alphaMethod1();
Note, that super
is a reference to the parent class, but super()
is its constructor.
Calling a base class's classmethod in Python
If you're using a new-style class (i.e. derives from object
in Python 2, or always in Python 3), you can do it with super()
like this:
super(Derived, cls).do(a)
This is how you would invoke the code in the base class's version of the method (i.e. print cls, a
), from the derived class, with cls
being set to the derived class.
Calling super super class method
You can't even use reflection. Something like
Class superSuperClass = this.getClass().getSuperclass().getSuperclass();
superSuperClass.getMethod("foo").invoke(this);
would lead to an InvocationTargetException
, because even if you call the foo-Method on the superSuperClass, it will still use C.foo()
when you specify "this" in invoke. This is a consequence from the fact that all Java methods are virtual methods.
It seems you need help from the B class (e.g. by defining a superFoo(){ super.foo(); }
method).
That said, it looks like a design problem if you try something like this, so it would be helpful to give us some background: Why you need to do this?
How do I call a parent class's method from a child class in Python?
Use the super()
function:
class Foo(Bar):
def baz(self, **kwargs):
return super().baz(**kwargs)
For Python < 3, you must explicitly opt in to using new-style classes and use:
class Foo(Bar):
def baz(self, arg):
return super(Foo, self).baz(arg)
Calling a superclass method from a subclass
The problem is with
return;
You should replace it by
return withdrawal;
public double makeWithdrawal(double withdrawal) {
double tempbalance = getBalance();
if (withdrawal > getBalance()) {
withdrawal = Input.getDouble("Your withdrawal cannot be larger than your balance. Enter a withdrawal <= "+getBalance());
return withdrawal;
}
else {
return super.makeWithdrawal(withdrawal);
}
}
How to call the superclass implementation of an overridden method from inside the superclass in Java?
You could move said method body to a private method and let the default method (the one which may be overridden by the subclass) delegate to the former. See this example
public abstract class SuperClass {
private Object field1;
protected SuperClass(Object obj){
// call the safe implementation
setField1Safe(obj);
}
public void setField1(Object obj){
// just delegates
setField1Safe(obj);
}
private void setField1Safe(Object obj){
// perform some check on obj and
// then set field1 such that field1==obj
}
}
public class SubClass extends SuperClass{
public SubClass(Object obj){
super(obj);
}
@Override
public void setField1(Object obj){
super.setField1(obj);
// do some work that is necessary only when
// field1 is set through SubClass.setField1()
}
}
That way the sub class can still override setField1
but if you really depend on the implementation then you can call the private setField1Safe
method.
Call super class method automatically
One way to do this is by making init()
final and delegating its operation to a second, overridable, method:
abstract class A {
public final void init() {
// insert prologue here
initImpl();
// insert epilogue here
}
protected abstract void initImpl();
}
class B extends A {
protected void initImpl() {
// ...
}
}
Whenever anyone calls init()
, the prologue and epilogue are executed automatically, and the derived classes don't have to do a thing.
How to call a parent class function from derived class function?
I'll take the risk of stating the obvious: You call the function, if it's defined in the base class it's automatically available in the derived class (unless it's private
).
If there is a function with the same signature in the derived class you can disambiguate it by adding the base class's name followed by two colons base_class::foo(...)
. You should note that unlike Java and C#, C++ does not have a keyword for "the base class" (super
or base
) since C++ supports multiple inheritance which may lead to ambiguity.
class left {
public:
void foo();
};
class right {
public:
void foo();
};
class bottom : public left, public right {
public:
void foo()
{
//base::foo();// ambiguous
left::foo();
right::foo();
// and when foo() is not called for 'this':
bottom b;
b.left::foo(); // calls b.foo() from 'left'
b.right::foo(); // call b.foo() from 'right'
}
};
Incidentally, you can't derive directly from the same class twice since there will be no way to refer to one of the base classes over the other.
class bottom : public left, public left { // Illegal
};
Related Topics
Ruby: Module, Require and Include
Rails: Testing Named Scopes with Rspec
Setting Up the Logger in Rails 3
Are There Any Ipython-Like Shells for Ruby or Rails
Copying Gems from Previous Version of Ruby in Rbenv
How to Dump an Http Request from Within Sinatra
Return Two and More Values from a Method
Finding Nil Has_One Associations in Where Query
How to Set an Attr_Accessor for a Dynamic Instance Variable
Restoring Rails 3's Bundle Install Path... It's Now Install in My Root
Failed to Build Gem Native Extension When Install Redcloth-4.2.9 Install Linux
How to Redefine a Ruby Constant Without Warning