Super Keyword in Java Programming

CWC
6 Min Read

We will discuss what is a super keyword in java programming. Super Keyword is an important part of any program. Without super keywords, we cannot run our program. We will discuss how to use the super keyword.

In java, there are different types of keywords. There are two types of keywords: Normal keyword and super keyword. The super keyword is more powerful and important than the normal keyword.

Here, we will discuss how to use the super keyword in the java program.

First, we will discuss what is super keyword?

The super keyword is a special keyword in java. This keyword has a special meaning. Without this keyword, we can’t use the super keyword.

For example:


Without super keyword we can't run this program.
public class Test
{
}
Without super keywords, we can't run this program.
public class Test
{
int i = 5;
}
without super keywords, we can't run this program.
public class Test
{
int i = 5;
}

The super keyword has two meanings. First, it is a keyword that belongs to the class. Second, it is a keyword that belongs to the method.

Java super keyword in Java is used for creating subclass instances of the current class. We can access superclass variables and methods in the subclass using the super keyword.

Example:


class Person{
int age;
public int getAge(){
return age;
}
void setAge(int age){
this.age=age;
}
}
class Student extends Person{
int score;
public int getScore(){
return score;
}
void setScore(int score){
this.score=score;
}
}
Person person=new Person();
student s=new Student();
s.setAge(22);
System.out.println("Person age : "+person.getAge());
System.out.println("Student age : "+s.getAge());
// Here we can access superclass variables like age and method using super keyword.
System.out.println("Superclass name : "+Person.class.getName());
System.out.println("Superclass method : "+Person.class.getMethod("getAge"));
System.out.println("Superclass object : "+Person.class.newInstance().getClass().getName());
}

How you use the super keyword in Java

The super keyword is useful in Java programs when you need to call a parent class method.


class Super {
public void printSomething() {
System.out.println("This is my print method");
}
}
class Sub extends Super {
public void printSomething() {
System.out.println("This is the print method from the subclass");
Super.printSomething();
}
}

Now try to create a new instance of the Sub class, and then call the method:


Super s = new Sub();
s.printSomething();

Super.printSomething() is called because the printSomething() method in the super class is being invoked.

Super keyword is also used to invoke the protected and private methods in the parent class. The keyword super will not be available in case of the final keyword.

For example:


class Subclass extends Superclass {
void printSomething() {
System.out.println("This is the print method from the subclass");
}
}

  • It will be used only when the method is not private or protected.
  • When the super keyword is used to invoke the private or protected methods, an error will occur.
  • So the super keyword is very useful in the JAVA programming language.
  • The super keyword is very useful in the JAVA programming language.

Another example

The super keyword is used to refer to the current object in a method. The keyword super refers to the direct superclass of an object. The keyword super allows you to access members and methods from the superclass of an object. When you use the keyword super, you can call only the superclass method, not the method defined in the current class. The syntax of using the super keyword is similar to calling a method.

In the following example, the variable name is the object of the Dog class.


public class Super {
Dog d = new Dog();
public void printName() {
System.out.println(d.getName()); // Calls the method getName from the class Dog.
}
}
public class Dog extends Animal{
String name;
// Constructor
public Dog() {
name = "Lucky";
}
public String getName() {
return name;
}
public void bark() {
System.out.println("Woof Woof");
}
}

When we try to call the method printName() on the dog object, we are getting the error message:

Error: No suitable method found for printName(Super)

Solution: We can solve this error by using the super keyword as follows:


public class Super {
Dog d = new Dog();
public void printName() {
System.out.println(d.getName()); // Calls the method getName from the class Dog.
}
}
public class Dog extends Animal{
String name;
// Constructor
public Dog() {
name = "Lucky";
}
public String getName() {
return name;
}
public void bark() {
System.out.println("Woof Woof");
}
}

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version