Java Method Overriding Super Keyword
Java Method Overriding Super Keyword
Now, if the same method is defined in both the superclass and the
subclass, then the method of the subclass class overrides the method of
the superclass. This is known as method overriding.
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Output:
I am a dog.
class Animal {
public void displayInfo() {
System.out.println("I am an animal.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Output:
I am an animal.
I am a dog.
However, we can call the constructor of the superclass from its subclasses.
For that, we use super() . To learn more, visit Java super keyword.
class Animal {
protected void displayInfo() {
System.out.println("I am an animal.");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.displayInfo();
}
}
Output:
I am a dog.
Java super
In this tutorial, we will learn about the super keyword in Java with the help
of examples.
class Animal {
// overridden method
public void display(){
System.out.println("I am an animal");
}
}
// overriding method
@Override
public void display(){
System.out.println("I am a dog");
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printMessage();
}
}
Output
I am a dog
In this example, by making an object dog1 of Dog class, we can call its
method printMessage() which then executes the display() statement.
Since display() is defined in both the classes, the method of
subclass Dog overrides the method of superclass Animal . Hence,
the display() of the subclass is called.
class Animal {
// overridden method
public void display(){
System.out.println("I am an animal");
}
}
// overriding method
@Override
public void display(){
System.out.println("I am a dog");
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printMessage();
}
}
Output
I am a dog
I am an animal
class Animal {
protected String type="animal";
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printType();
}
}
Output:
I am a mammal
I am an animal
In this example, we have defined the same instance field type in both the
superclass Animal and the subclass Dog .
We then created an object dog1 of the Dog class. Then,
the printType() method is called using this object.
Inside the printType() function,
type refers to the attribute of the subclass Dog .
super.type refers to the attribute of the superclass Animal.
Hence, System.out.println("I am a " + type); prints I am a mammal .
class Animal {
System.out.println("I am a dog");
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
}
}
Output
I am an animal
I am a dog
Here, when an object dog1 of Dog class is created, it automatically calls the
default or no-arg constructor of that class.
Inside the subclass constructor, the super() statement calls the constructor
of the superclass and executes the statements inside it. Hence, we get the
output I am an animal .
The flow of the program then returns back to the subclass constructor and
executes the remaining statements. Thus, I am a dog will be printed.
However, using super() is not compulsory. Even if super() is not used in the
subclass constructor, the compiler implicitly calls the default constructor of
the superclass.
So, why use redundant code if the compiler automatically invokes
super()?
It is required if the parameterized constructor (a constructor that takes
arguments) of the superclass has to be called from the subclass
constructor.
The parameterized super() must always be the first statement in the body of
the constructor of the subclass, otherwise, we get a compilation error.
Example 5: Call Parameterized Constructor Using super()
class Animal {
// default or no-arg constructor
Animal() {
System.out.println("I am an animal");
}
// parameterized constructor
Animal(String type) {
System.out.println("Type: "+type);
}
}
// default constructor
Dog() {
System.out.println("I am a dog");
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
}
}
Output
Type: Animal
I am a dog
SUMBER: PROGRAMIZ.COM