answersLogoWhite

0

Why we use super in java?

Updated: 8/11/2023
User Avatar

Wiki User

12y ago

Best Answer

The keyword super is used to refer to the parent class instance of the current class.

Lets say we have a class

class A extends B {

...

public void getName(){

}

...

}

Lets assume the parent class B also has a method getName()

Inside class A if you call getName() it would by default call the current class's method. To make the JVM intentionally call the super class method we can use super

if we say super.getName() then the parent class instance of the method would be called.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

super keyword is used for two reasons:

1. To Call the super class or base class constructor

2.To call super class methods.

If you don't create constructor in a class...Java automatically put for you.

Always remember..that the first statement in derived class constructor must be a call to super or this.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

The super keyword is used to refer to the superclass of the current class. Common uses include when extending from a Component. The paint() method may need to be overridden, but the parent's paint method still needs to be called.

class MyFrame extends JFrame() {

public void paint(Graphics g) {

super.paint(g);

// do our specialized paint here

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Super refers to the parent class object instance. In case of static methods, they do not belong to any object instance and hence they cannot call anything related to the parent class instance.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

These are two different things.

"Super" is used in inheritance - if class B inherits from class A, and we want the constructor of B to do the same things the constructor of A does (and maybe more things), we can call A's constructor in B's constructor:

public B() {

super();

......

}

Notice that it must be the first line in B's constructor.

"This" is used in case of overloading, to call a function aith the same name.

For example, if we have a constructor that receives one argument, and we it want to have a default value of 0, we can write:

public C() {

this(0);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

"super " is a key word in java. this is used for referring to super class members ( variables and methods ) from subclass .( when sub class and super class have members with same names).

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Super refers to the base class of your object. For example, if you have a class named Foo...

@Override

public String getClassName()

{

String thisClass = "Foo";

String baseClass = super.getClassName();

return thisClass + ", which derives from " + baseClass;

}

...which derives from base class Bar...

public String getClassName()

{

return "Bar";

}

... calling Bar.getClassName would print "Bar", while calling Foo.getClassName would print "Foo, which derives from Bar".

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

super is a keyword in java.uses of super are:

1.it is used to call super class constructor.

2.It is used to call a method of super class that is hidden by sub-class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why we use super in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is java 7?

Java 7 is the most current version of Java. Many movies and games use Java.


Do you have to use java?

Not usually. There are numerous languages out there with similar functionality. The only time you must use Java is if your employer or client demands that a project be done in Java, or a particular platform requires the use of Java, such as Android OS.


What are the Names of header files in java?

Java doesn't use header files.


Can you do java keylogger?

Yes, it is possible to write a keylogger in Java, but you would need to use the Java Native Interface to do so.


What is a connection class in java?

to connect java and data base...use connection stament

Related questions

Can you use pointers in java?

Java does not support Pointers and hence you cannot use it in Java.


Why Object is Super Most Class In Java?

It's part of the language specification that all objects in Java must inherit from Object. Java defines a strict class hierarchy, and enforcing a "super most class" ensures that this ordering is maintained.


What technologies link to Java?

Java is exclusive to surfing the web. Some web sites use Java to embed videos, others use Java for advertisements.


Does Java Script use the same compiler as Java?

No


What you use in case of pointer in java?

Java does not have pointers


What is java 7?

Java 7 is the most current version of Java. Many movies and games use Java.


Which is the best Book to understand JAVA in short?

You can use Head First Java or Java Complete Reference to learn Java.


What happens when no constructor function is declared in a class - in Java?

When any constructor is deffined in your class, the java compiler create a default no argument constructor for you. This constructor only have an invocation to the super class constructor (" super( ) ").


What is the program to use the super and final keyword in java?

Many programs use those keywords.super refers to the direct superclass of the class referencing itfinal declares a variable or class unchangable


Do you have to use java?

Not usually. There are numerous languages out there with similar functionality. The only time you must use Java is if your employer or client demands that a project be done in Java, or a particular platform requires the use of Java, such as Android OS.


How do you script a RuneScape bot?

RuneScape bots use Java. Use a Java Binary Code.


Why you not use clrscr in java?

Many reasons, one of them: there is no 'clrscr' in Java.