If you are new to object-oriented programming, understanding the ways to call an overridden superclass method in a subclass can be a bit of challenging. Fortunately, there are a few simple options that make it easy to harness the power of calling a superclass method.
In this post, we'll go over the basics of how to call an overridden superclass method from a subclass, as well as provide a few helpful examples that you can use to give yourself a better understanding of this concept. Let's get started!
Using super
A common way to call an overridden superclass method in a subclass is by using the super
keyword. super
refers to the parent class and allows you to pass the name of the superclass method you want to call along with any arguments that need to be passed.
Here's an example of how you would call the foo()
method from your superclass (assuming the method has arguments):
super.foo(arg1, arg2)
Using the ParentName.super
Constructor
You can also call an overridden superclass method from a subclass by using the ParentName.super
constructor. This construct takes the the parent class name and uses it to create an instance of the superclass.
Here's an example of how you would call the foo()
method from your superclass (assuming the method has arguments):
MyParentClass.super.foo(arg1, arg2)
Use of this.getClass().getSuperclass()
This is an advanced way to call an overridden superclass method from a subclass by making use of the this.getClass().getSuperclass()
method. This will call a constructor in the superclass, allowing you to access all of its methods.
Here's an example of how you would call the foo()
method from your superclass (assuming the method has arguments):
this.getClass().getSuperclass().foo(arg1, arg2)
Lookup Method Resolution Order
Finally, you can also call an overridden superclass method from a subclass by using the lookup method resolution order (MRO). This is a process that will allow you to search through your parent classes to find a method that matches the name of the method you are looking for.
Here's an example of how you would use the lookup method resolution order to call the foo()
method from your superclass (assuming the method has arguments):
mro().lookup("foo", arg1, arg2)
FAQs
What is a superclass?
A superclass is a class from which one or more other classes are derived from. It contains all the methods and attributes that are also present in its child classes, but with the feature that they can be inherited and reused.
What is a subclass?
A subclass is a class that is derived from another, or multiple, classes that it inherits the functionality and properties from. It can include the same methods and attributes as its superclass, but with the addition of further details and customizations.
What is the difference between subclass and superclass?
The primary difference between a subclass and superclass is that a subclass is derived from a superclass, while a superclass is the original class.
A subclass can include the same methods and attributes as its superclass, but with the addition of further details and customizations. A superclass, on the other hand, sets the basic foundation for the class and all subclasses that are derived from it.
How do you inherit a superclass in Java?
In order to inherit a superclass in Java, you simply need to use the extends keyword in your subclass. This will allow your subclass to inherit all of the methods and attributes from your superclass.
How do you override a superclass method in Java?
In order to override a superclass method in Java, you need to use the override annotation. This annotation will allow the method to be superseded by the defined method in the subclass.