What to do with super in java?
OR define constructor in class You only need to define the special constructor in Coin class if you want to do something different. av class was defined as below super() invokes the immediate parent class constructor -> this I know Parent class is Object.
What does super() do here
I was going through a piece of code in an android Malware to understand its working. av class was defined as below
public final class av implements android.widget.AdapterView.OnItemClickListener < final ChooserActivity a; public av(ChooserActivity chooseractivity) < super(); a = chooseractivity; >. Other methods here >
super() invokes the immediate parent class constructor -> this I know
Parent class is Object. So the constructor will return what. Meaning what will be the properties of returned object.?
super() must be in the first statement inside the subclass constructor ,it calls the parent constructor
public av(ChooserActivity chooseractivity)
It is a call up to the parent constructor, the parent constructor of ac is Object .
Object is the default class your object extends from and it’s constructor does nothing. ( it actually doesn’t have one )
Also you must call super() as the first line of your constructor, and by convention you should name your classes with an uppercase character.
public final class Av implements android.widget.AdapterView.OnItemClickListener < final ChooserActivity a; public Av(ChooserActivity chooseractivity) < super(); this.a = chooseractivity; >>
but because the class you are extending is Object you actually don’t need to call super.
public final class Av implements android.widget.AdapterView.OnItemClickListener < final ChooserActivity a; public Av(ChooserActivity chooseractivity) < this.a = chooseractivity; >>
You should probably read up on Understanding Java Constructors
The code you have listed is invalid and will not compile. The call to super() must be the first statement in the constructor. It is used to construct the object you are extending before you go on and construct yourself.
This code will not even compile . Remember constructor call must be the first statement in a constructor.
public av(ChooserActivity chooseractivity) < // Call the base class constructor. Should be the firs super(); a = chooseractivity; >
public av(ChooserActivity chooseractivity) < // Compiler will give implicit call to the base class contructor a = chooseractivity; >
Java super Keyword, The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name. To understand the super …
What to do with super in java?
Design and implement a class called MonetaryCoin that is derived from the Coin class presented in Chapter 5. Store a value in the monetary coin that represents its value and add getter and setter methods for the monetary value.
The Coin class is as follows:
public class Coin < public final int HEADS = 0; public final int TAILS = 1; private int face; // --------------------------------------------- // Sets up the coin by flipping it initially. // --------------------------------------------- public Coin () < flip(); >// ----------------------------------------------- // Flips the coin by randomly choosing a face. // ----------------------------------------------- public void flip() < face = (int) (Math.random() * 2); >// --------------------------------------------------------- // Returns true if the current face of the coin is heads. // --------------------------------------------------------- public boolean isHeads() < return (face == HEADS); >// ---------------------------------------------------- // Returns the current face of the coin as a string. // ---------------------------------------------------- public String toString() < String faceName; if (face == HEADS) faceName = "Heads"; else faceName = "Tails"; return faceName; >>
public class MonetaryCoinHW extends Coin < public MonetaryCoinHW(int face) < setFace(face); >public int getFace() < if (isHeads()) < return HEADS; >return TAILS; > public void setFace( int newFace ) < while (newFace != getFace()) < flip(); >>
However, I keep getting syntax errors. Am I not using «super» correctly? I am completely confused; what is my mistake?
No, you are not using super() correctly.
super() calls the superconstructor — in this case, it will call the inherited Coin() . Since no constructor exists inside Coin() for Coin(int face) , your subclass can’t invoke it.
There are to ways to deal with this. I believe you need to run setFace(face) . This will properly initialize the value of the coin, and I think fits your problem the best. However, you could also add the Coin(int face) constructor to the Coin class. You would also have to give the Coin a way to hold a value, though.
The class coin needs to have the following constructor for your sub-class to work correctly.
This will solve the problem super(face).
No you are not calling super correctly. You need to have constructor of coin with one argument that is int. i.e.
remove the super(face) line from you class MonetaryCoinHW . And simple call setFace(face) . But also note that the face variable is defined private in your super class so you cannot access it.
By default it will call the no argument constructor of super class.
define Coin(int face) constructor in class Coin
You only need to define the special constructor in Coin class if you want to do something different.
Java — Why call super() in a constructor?, A call to your parent class’s empty constructor super () is done automatically when you don’t do it yourself. That’s the reason you’ve never had to do it in your code. It was done for you. When your superclass doesn’t have a no-arg constructor, the compiler will require you to call super with the appropriate …
Confused what the «super» keyword is doing in this context
I am still trying to learn java, so correct me if I’m wrong. As I understand it, the super keyword calls the constructor of a class. So I do not understand what the significance of saying
does for example, or more recently while reading about animation I came across
I tried looking up documentation on the two (perhaps poorly) and it didn’t satisfy my curiosity adequately, so here we are.
Any clarification on those two example or in general would be appreciated. Thanks!
super is a keyword that is a reference to the superclass of the class from which you are using the keyword.
So super() calls the constructor of the superclass, super(x) calls a parameterised constructor, and super.paint(g) calls the paint() method of the superclass.
No, the super keyword refers to the base class. So super.f() calls the f() method of the superclass.
In some cases, the superclass privides some basic routines, and when you override the function, you first call the base class’s implementation, and then add your custom code. It’s the case for both your examples.
Let me describe it in code:
public class Parent < protected void doSomething()< . >> //notice the extends Parent which is the super public class Child extends Parent < public void doSomethingOnParent()< super.doSomething(); //calls a method on the parent class >>
public class A < public void pirintStr()< System.out.print("Hello from A"); >> public class B extends A < public void fun()< // if you wirte this printStr(); //this will print Hello from B //if you want the super class version of this method you can write super keyword super.printStr();//this will print Hello from A >public void printStr() < System.out.print("Hello from B"); >>
Java — confused what the «super» keyword is doing in, I am still trying to learn java, so correct me if I’m wrong. As I understand it, the super keyword calls the constructor of a class. So I do not understand what the significance of saying super.paint(g) does for example, or
Using the Keyword super
If your method overrides one of its superclass’s methods, you can invoke the overridden method through the use of the keyword super . You can also use super to refer to a hidden field (although hiding fields is discouraged). Consider this class, Superclass :
Here is a subclass, called Subclass , that overrides printMethod() :
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 Subclass , the simple name printMethod() refers to the one declared in Subclass , which overrides the one in Superclass . So, to refer to printMethod() inherited from Superclass , Subclass must use a qualified name, using super as shown. Compiling and executing Subclass prints the following:
Printed in Superclass. Printed in Subclass
Subclass Constructors
The following example illustrates how to use the super keyword to invoke a superclass’s constructor. Recall from the Bicycle example that MountainBike is a subclass of Bicycle . Here is the MountainBike (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)
Invocation of a superclass constructor must be the first line in the subclass constructor.
The syntax for calling a superclass constructor is
With super() , the superclass no-argument constructor is called. With super(parameter list) , 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. Object does have such a constructor, so if Object 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 Object . 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.
What is super function in java
- Introduction to Java
- The complete History of Java Programming Language
- C++ vs Java vs Python
- How to Download and Install Java for 64 bit machine?
- Setting up the environment in Java
- How to Download and Install Eclipse on Windows?
- JDK in Java
- How JVM Works – JVM Architecture?
- Differences between JDK, JRE and JVM
- Just In Time Compiler
- Difference between JIT and JVM in Java
- Difference between Byte Code and Machine Code
- How is Java platform independent?
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Java if statement with Examples
- Java if-else
- Java if-else-if ladder with Examples
- Loops in Java
- For Loop in Java
- Java while loop with Examples
- Java do-while loop with Examples
- For-each loop in Java
- Continue Statement in Java
- Break statement in Java
- Usage of Break keyword in Java
- return keyword in Java
- Object Oriented Programming (OOPs) Concept in Java
- Why Java is not a purely Object-Oriented Language?
- Classes and Objects in Java
- Naming Conventions in Java
- Java Methods
- Access Modifiers in Java
- Java Constructors
- Four Main Object Oriented Programming Concepts of Java
- Inheritance in Java
- Abstraction in Java
- Encapsulation in Java
- Polymorphism in Java
- Interfaces in Java
- ‘this’ reference in Java