Week 10 — What are the main uses of the super keyword?
Question of the Week #10
What are the main uses of the super keyword?
1 Reply
accepted answer from
quadflame#5250
:
The super keyword is used to access extended class objects known as superclasses objects. Common examples can be used to invoke methods, variables and constructors e.g. The super keyword is also very useful when it comes to overriding an existing method then performing the function in the superclass, for example: If you were to call the printVulnerabilities() method in a Tesla instance the console will print: accepted answer fromrajBhog🍨#2587
: It allows one to use the constructor of the parent class from child classes constructor. Mostly used when parent class constructor does something useful which child class is using. super(input) is done to call the constructor of parent class accepted answer fromimraklr#1712
: super keyword is used in Java to: 1) call the constructor of the super class. It is the first call in the constructor of the derived class. 2) to access public data member of super class in the derived class 3) to bound the provided generic type by the lower bound, super is used(extends is used for higher bound) accepted answer fromMinecraftMan1013#7242
: If your class extends another class, you can callsuper()
inside of your constructor, then you can access your extended class's constructor. Names.java Activation.java accepted answer bydan1st#7327
: Thesuper
keyword is used for accessing members of super classes and for calling the super constructor. As an example, take this class: It is now possible to create a child class extendingSuperClass
. Since the constructor ofSuperClass
contains arguments, the constructor ofChildClass
needs to call the constructor ofSuperClass
using thesuper
keyword. The constructor of the parent class can be called usingsuper(arguments, go, here);
where any arguments in thesuper()
call get passed to the parent class. Calling the super constructor must be the first statement of the constructor. It is also possible to refer to methods and fields of the parent class using by prefixing them withsuper.
. When callingsuper.someMethod()
, that method is called on the super class instead of calling the overridden method (if present). The keywordsuper
can also be used to explicitely refer to default methods of interfaces ignoring any overrides. When calling a method usingInterfaceName.super.theMethod()
, it callstheMethod()
declared insomeInterface
and not any potential overrides. accepted answer byAussied#7593
: super() is basically used to give parameters or variables to another constructor, there are probably other ways to use it but this way is used a lot more. And the parameter can be set by a manager as an example or it can just be used like this accepted answer byHarshit_Nagpal#1501
: Java Scanner class is part of the java.util package. The Scanner is mostly used to receive user input and parse them into primitive data types such as int, double or default String. It’s a utility class to parse data using regular expressions by generating tokens. Java Scanner Class Constructor Most of the constructors are using one of the three objects: InputStream - the most common where we pass System.in to receive user input. File or Path - We can scan file data too and work with the values from the file. String - We can create a scanner for a string source too and parse values from it. Important methods of scanner class hasNext() - returns true if there is another token in the input. It’s a blocking method and it will keep waiting for user input. next() - returns the next token from the scanner. It’s used in conjunction with the hasNext() method. close() - scanner is resource heavy, so once you are done with it, use this method to close it and release system resources. 1. Reading user input This is the most common use of the Scanner class. We can instantiate with System.in as input source and read the user input. Scanner sc = new Scanner(System.in); sc.useDelimiter(System.getProperty("line.separator")); System.out.println("Please enter your name"); String name = sc.next(); System.out.println("Hello " + name); sc.close(); best answer byKarter#4951
: Thesuper
keyword in Java is used to signifies hierarchy between classes and has more than one use case. Firstly, it's used in the non-static context of a method to reference the parent instance ofthis
. This is normally used when working with method overriding sincesuper
can be used to reference original parent methods that have been overrides by the child class. This can be useful if the parent solution overlaps with the child solution. Here is an example. Here, the child class extends the functionality of the print "Becky is eating" by specifying the object that Becky is eating "an apple" or "a carrot". Super is also used in generics. In wildcard bounding, super is used to reference the parameterized type as super to the specified type, giving the parameterized type a lower bound. Here is an example of a lower bounded wildcard. In this case, super is used to create a lower bound that provides security to the proceeding operation since,List<? extends Animal>
may result in a compilation error if the List wasList<Dog>
because not all animals are dogs.