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
dan1st
dan1st2y ago
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.
super(param1);
super(param1);
super.onEnable();
super.onEnable();
super.var6 = 4;
super.var6 = 4;
The super keyword is also very useful when it comes to overriding an existing method then performing the function in the superclass, for example:
public class Tesla extends Car {

@Override
public void printVulnerabilities() {
System.out.println("Car may run out of power");
super.printVulnerabilities();
}
}


public class Car {

public void printVulnerabilities() {
System.out.println("Tyres may pop");
}
}
public class Tesla extends Car {

@Override
public void printVulnerabilities() {
System.out.println("Car may run out of power");
super.printVulnerabilities();
}
}


public class Car {

public void printVulnerabilities() {
System.out.println("Tyres may pop");
}
}
If you were to call the printVulnerabilities() method in a Tesla instance the console will print:
Car may run out of power
Tyres may pop
Car may run out of power
Tyres may pop
accepted answer from rajBhog🍨#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 from imraklr#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 from MinecraftMan1013#7242: If your class extends another class, you can call super() inside of your constructor, then you can access your extended class's constructor. Names.java
publc class Name {
private String first;
private String last;
private double age;

public Name(String first, String last, double age) {
this.first = first;
this.last = last;
this.age = age;
}

// other getter methods
}
publc class Name {
private String first;
private String last;
private double age;

public Name(String first, String last, double age) {
this.first = first;
this.last = last;
this.age = age;
}

// other getter methods
}
Activation.java
public class ActivateName extends Name {
public ActivateName() {
super("John", "Cena", 45.17808219178);
}
public class ActivateName extends Name {
public ActivateName() {
super("John", "Cena", 45.17808219178);
}
accepted answer by dan1st#7327: The super keyword is used for accessing members of super classes and for calling the super constructor. As an example, take this class:
public class SuperClass{
protected int someInt;
private String someString;
public SuperClass(int someInt, String someString){
this.someInt=someInt;
this.someString=someString;
}
public String getSomeString(){
return someString;
}
}
public class SuperClass{
protected int someInt;
private String someString;
public SuperClass(int someInt, String someString){
this.someInt=someInt;
this.someString=someString;
}
public String getSomeString(){
return someString;
}
}
It is now possible to create a child class extending SuperClass. Since the constructor of SuperClass contains arguments, the constructor of ChildClass needs to call the constructor of SuperClass using the super keyword. The constructor of the parent class can be called using super(arguments, go, here); where any arguments in the super() 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 with super.. When calling super.someMethod(), that method is called on the super class instead of calling the overridden method (if present).
public class ChildClass extends SuperClass{
public ChildClass(){
//statements before the super() call would be invalid
super(1337, "World");//call parent constructor
}
@Override
public String getSomeString(){
return "Hello "+super.getSomeString();//super.getSomeString() refers to getSomeString() in SuperClass while this.getSomeString() would refer to the method in ChildClass
}
public int getSomeInt(){
return super.someInt;//super is optional here
}
public void printValues(){
System.out.println("Super string: "+super.getSomeString());//Super string: World
System.out.println("without super: "+getSomeString());//without super: Hello World
System.out.println("int: "+getSomeInt());//int: 1337
}
}
public class ChildClass extends SuperClass{
public ChildClass(){
//statements before the super() call would be invalid
super(1337, "World");//call parent constructor
}
@Override
public String getSomeString(){
return "Hello "+super.getSomeString();//super.getSomeString() refers to getSomeString() in SuperClass while this.getSomeString() would refer to the method in ChildClass
}
public int getSomeInt(){
return super.someInt;//super is optional here
}
public void printValues(){
System.out.println("Super string: "+super.getSomeString());//Super string: World
System.out.println("without super: "+getSomeString());//without super: Hello World
System.out.println("int: "+getSomeInt());//int: 1337
}
}
The keyword super can also be used to explicitely refer to default methods of interfaces ignoring any overrides. When calling a method using InterfaceName.super.theMethod(), it calls theMethod() declared in someInterface and not any potential overrides.
public interface SomeInterface{
default void someMethod(){
System.out.println("default method of interface called");
}
}
public class SomeClass implements SomeInterface{
@Override
public void someMethod(){
System.out.println("overridden method of class called");
}
public void check(){
someMethod();//overridden method of class called
SomeInterface.super.someMethod();//default method of interface called
}
}
public interface SomeInterface{
default void someMethod(){
System.out.println("default method of interface called");
}
}
public class SomeClass implements SomeInterface{
@Override
public void someMethod(){
System.out.println("overridden method of class called");
}
public void check(){
someMethod();//overridden method of class called
SomeInterface.super.someMethod();//default method of interface called
}
}
accepted answer by Aussied#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.
public class Fullbright extends Mod {

public Fullbright(String name) {
super(name); // Giving the variable to the constructor from Mod
}
public class Fullbright extends Mod {

public Fullbright(String name) {
super(name); // Giving the variable to the constructor from Mod
}
And the parameter can be set by a manager as an example
public ModManager() {
modsList.add(new Fullbright("Fullbright");
}
public ModManager() {
modsList.add(new Fullbright("Fullbright");
}
or it can just be used like this
public Fullbright() {
super("Fullbright");
}
public Fullbright() {
super("Fullbright");
}
accepted answer by Harshit_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 by Karter#4951: The super 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 of this. This is normally used when working with method overriding since super 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.
public class A {
public void display() {
System.out.println("Becky is eating");
}
}

public class B extends A {

@Override
public void display() {

super.display();
System.out.println(" an apple");
}
}
public class C extends A {

@Override
public void display() {

super.display();
System.out.println(" a carrot");
}
}
public class A {
public void display() {
System.out.println("Becky is eating");
}
}

public class B extends A {

@Override
public void display() {

super.display();
System.out.println(" an apple");
}
}
public class C extends A {

@Override
public void display() {

super.display();
System.out.println(" a carrot");
}
}
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.
public static void addAnimal(List<? super Animal> List list) {

list.add(new Animal("Siberian Tiger"));
}
public static void addAnimal(List<? super Animal> List list) {

list.add(new Animal("Siberian Tiger"));
}
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 was List<Dog> because not all animals are dogs.
Want results from more Discord servers?
Add your server