What is super () in java with example
What is super () in java with example
I'd be happy to help you with that!
In Java, the super
keyword refers to the parent class of an object. When used as a method invocation or constructor invocation, it calls a method or constructs an instance from the superclass.
Let's take a simple example to understand this better:
class Animal {
void sound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calls the overridden method in Animal class
System.out.println("The dog barks");
}
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
}
}
In this example, we have an Animal
class with a sound()
method that prints out a generic message. We also have a Dog
class that extends the Animal
class and overrides the sound()
method.
When we call myDog.sound()
, it will first print out "The animal makes a sound" because of the super.sound()
invocation, which calls the overridden method in the superclass (in this case, Animal
). Then it prints out "The dog barks", which is its own unique behavior.
In Java, you can use super
to:
class Animal {
void sound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calls the method in Animal class
System.out.println("The dog barks");
}
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
}
}
Access fields from the superclass:
class Animal {
int age;
public Animal(int initialAge) {
this.age = initialAge;
}
}
class Dog extends Animal {
@Override
public Dog(int initialAge) {
super(initialAge); // Calls the constructor in Animal class
}
public static void main(String[] args) {
Dog myDog = new Dog(3);
System.out.println("My dog's age is " + myDog.age);
}
}
In summary, super
is used to access methods and fields from a superclass or parent class in Java. It can be used as a method invocation or constructor invocation to call overridden methods or constructors from the superclass.
I hope that helps you understand super
better! Let me know if you have any other questions.
What is super () in java w3schools
I'm happy to help! However, I must point out that W3Schools is a website focused on web development and programming languages like HTML, CSS, JavaScript, and PHP, whereas super() is a method-related concept specific to Java.
In Java, the super
keyword is used to access the methods or fields of the superclass (parent class). It's an instance of the superclass that can be used to invoke any method or get the value of any field in the superclass. The super
keyword is particularly useful when you're overriding a method in a subclass.
Here are some examples of using the super
keyword in Java:
Invoking a superclass method
Let's say we have a class called Animal
with a method called sound()
, and a subclass called Dog
that inherits from Animal
. We can override the sound()
method in Dog
to produce a specific sound, like "Woof!":
public class Animal {
public void sound() {
System.out.println(" Generic animal sound!");
}
}
public class Dog extends Animal {
@Override
public void sound() {
super.sound(); // Calls the sound() method of Animal
System.out.println(" Woof!");
}
}
In this example, when we call dog.sound()
and dog
is an instance of Dog
, it will first print "Generic animal sound!" (the superclass's output) and then "Woof!".
Accessing a superclass field
Similarly, if you have a superclass with a field that you want to access from the subclass, you can use super
:
public class Animal {
public String habitat = "Forest";
}
public class Dog extends Animal {
@Override
public void describe() {
System.out.println("I'm a dog. My habitat is " + super.habitat + ".");
}
}
In this case, super.habitat
returns the value of the habitat
field from the superclass Animal
.
Note: The super
keyword can only be used to access methods or fields that are declared in the superclass. If a method is overridden in the superclass and you want to call the original method (the one from the parent class), you should use super.originalMethod()
.
I hope this helps! Let me know if you have any more questions.