What is the use of the keyword super is it absolutely necessary to use super?
If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword Show
public class Superclass { public void printMethod() { System.out.println("Printed in Superclass."); } } Here is a subclass, called public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } } Within Printed in Superclass. Printed in Subclass Subclass ConstructorsThe following example illustrates how to use the public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }9 example that Printed in Superclass. Printed in Subclass0 is a subclass of public class Subclass extends Superclass { // overrides printMethod in Superclass public void printMethod() { super.printMethod(); System.out.println("Printed in Subclass"); } public static void main(String[] args) { Subclass s = new Subclass(); s.printMethod(); } }9. Here is the Printed in Superclass. Printed in Subclass0 (subclass) constructor that calls the superclass constructor and then adds initialization code of its own: public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) { super(startCadence, startSpeed, startGear); seatHeight = startHeight; } Invocation of a superclass constructor must be the first line in the subclass constructor. The syntax for calling a superclass constructor is or:With Printed in Superclass. Printed in Subclass3, the superclass no-argument constructor is called. With Printed in Superclass. Printed in Subclass4, the superclass constructor with a matching parameter list is called. Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error. Printed in Superclass. Printed in Subclass5 does have such a constructor, so if Printed in Superclass. Printed in Subclass5 is the only superclass, there is no problem. If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that there will be a whole chain of constructors called, all the way back to the constructor of Printed in Superclass. Printed in Subclass5. In fact, this is the case. It is called constructor chaining, and you need to be aware of it when there is a long line of class descent. Inheritance is a core feature in object-oriented programming that allows a developer to define a new class from an existing class. For instance, a class to store the suppliers of meat at a local health foods store could be defined based on a class to store any product sold at the store. When you’re working with inheritance in Java, you’ll likely encounter the super keyword. The super keyword is used to access superclass members inside a subclass, and has a variety of uses when inheriting superclass members. Find Your Bootcamp Match
First name Last name Phone number By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email. This tutorial will discuss, with references and examples, the basics of the Java super keyword and how you can use it in your code. By the end of reading this tutorial, you’ll be a master at using the super keyword in Java. Java InheritanceIn Java, inheritance is used to define a new class from an existing one. Inheritance is a useful feature because it allows coders to reduce the amount of code they have to repeat because they can use existing code to create new classes. Inheritance is used when there is an
By inheriting code from another method, you can access the attributes, constructors, and methods stored in that function and use those in your new method. Here’s an example of inheritance being used to inherit the code stored in a BankAccount method for use in a new method called SavingsAccount: class BankAccount { // Methods, attributes, constructors } class SavingsAccount extends BankAccount { // Methods, attributes, constructors } In this example, we use the extends keyword to inherit all the methods, attributes, and constructors from the BankAccount method and apply them to the new SavingsAccount method. If you’re interested in learning more about Java inheritance, you can read our full guide on the topic here. » MORE: How to Use Enum in Java Java SuperThe super keyword is used to access superclass members in a subclass. The members of a superclass are constructors, attributes, and methods. In Java, the superclass, also known as the The super keyword is used in three situations, which we will explore below. Using Super to Access Superclass AttributesIn Java, attributes in a superclass and subclass can have the same name. If you want to explicitly access the contents of a superclass’ attribute, you have to use the super keyword. Here’s an example of super being used with reference to our bank account example from above to access a class attribute: class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } } Our code returns: Account type: Savings Account Parent account type: Bank Account In our example, we have declared a superclass called BankAccount which has one attribute: accountType. accountType is equal to We have also defined a subclass called SavingsAccount which inherits its values from BankAccount. In this subclass, we declare a variable called accountType which stores the type of account held by that class, which is class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }0 in this case. Then we create a method called class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }1 which, when invoked, prints out the account type of the SavingsAccount, preceded by the label class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }2. We use the accountType variable to print out the account type of SavingsAccount because the accountType variable has local scope. » MORE: Exception Handling with Try Catch in Java After that has been printed, class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }3 is printed to the console, followed by the accountType of the BankAccount class. We use class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }4 to reference the value of accountType which is stored in the parent class default constructor. In our main program, we first create an object of the SavingsAccount method called johnAccount which stores the details for his account. Then we call the method called class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }1 to retrieve information about John’s account. Using Super to Access Superclass MethodMethods in a subclass and a superclass can have the same name. If you want to access the method stored in a superclass that has the same name as a subclass method, you have to use the super keyword. Here’s an example of super being used to access a superclass method, using the example of a program that stores bank account information: class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } } Our code returns: Account type: Savings Account Account type: Bank Account In this example, we have declared a superclass called BankAccount which has one method: class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6. When this method is invoked, class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }7 is printed to the console. We have also declared a subclass called SavingsAccount which has two methods: class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6 and class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }9. When class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6 is executed, Account type: Savings Account Parent account type: Bank Account1 is printed to the console. When class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }9 is executed, we use the super keyword to invoke the class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6 method from our parent class. So, instead of executing the class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6 method in the SavingsAccount class, our program executes the class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6 method in the BankAccount class. In our main program, we first declare an instance of SavingsAccount called johnSavings. We then invoke the class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6 method, which runs class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }6 from the SavingsAccount method. Finally, we invoke class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }9 which executes Account type: Savings Account Parent account type: Bank Account9 and prints class BankAccount { protected String accountType = "Bank Account"; } class SavingsAccount extends BankAccount { public String accountType = "Savings Account"; public void printAccountDetails() { System.out.println("Account type: " + accountType); System.out.println("Parent account type: " + super.accountType); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.printAccountDetails(); } }7 to the console. » MORE: What Is a Java Class? Using Super to Access Superclass ConstructorThe super keyword is also used to access a constructor from a superclass that shares the same name as a constructor in a subclass. You can learn more about Java constructors in our Java constructor tutorial here. To call a superclass constructor from a subclass constructor, we need to use class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }1 inside the subclass constructor. class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }2 must be the first statement in the argument constructor. "Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!" Venus, Software Engineer at Rockbot Find Your Bootcamp MatchHere’s an example of class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }1 being used to call a superclass constructor, using our bank account example application: class BankAccount { BankAccount() { System.out.println("Parent account type: Bank Account"); } } class SavingsAccount extends BankAccount { SavingsAccount() { super(); System.out.println("Main account type: Savings Account"); } } class Main { public static void main(String[] args) { SavingsAccount johnAccount = new SavingsAccount(); } } Our code returns: Parent account type: Bank Account Main account type: Savings Account In this example, we have defined a superclass called BankAccount which includes a constructor. This constructor, when invoked, prints class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }4 to the console. We have also defined a subclass called SavingsAccount which includes a constructor. This constructor, when invoked, uses class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }1 to invoke the BankAccount constructor in the BankAccount class. This prints class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }4 to the console. Then, the SavingsAccount constructor prints class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }7 to the console. In our main program, we initialize an instance of the SavingsAccount object called johnAccount. When we initialize johnAccount, the class BankAccount { public void displayAccountType() { System.out.println("Account type: Bank Account"); } } class SavingsAccount extends BankAccount { @Override public void displayAccountType() { System.out.println("Account type: Savings Account"); } public void displayParentAccountType() { super.displayAccountType(); } } class Main { public static void main(String[] args) { SavingsAccount johnSavings = new SavingsAccount(); johnSavings.displayAccountType(); johnSavings.displayParentAccountType(); } }8 constructor is executed. ConclusionThe Java super keyword has three uses. These are to:
This tutorial discussed, with reference to examples, how to use the Java superclass keyword for those purposes. Now, after reading this tutorial, you’re ready to start using the Java super keyword like an expert! 1 Ratings About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication. Is it absolutely necessary to use super in Java?The Super keyword is very useful when both child and parent classes have the same data members. If both child and parent classes have the same data member, then there is a possibility of ambiguity for Java Virtual Machine(JVM) which can be avoided using super keyword. For example, consider the code snippet.
What is the keyword super used for?The super keyword is used to access properties on an object literal or class's [[Prototype]], or invoke a superclass's constructor.
Why super keyword is used in C++?Microsoft Visual Studio implements the __super keyword for C++ whether or not it's for CLI. If only one of the inherited classes offers a method of the correct signature (the most common case, as mentioned by Tanktalus) then it just picks the only valid choice.
|