Back
What is the difference between this and super keywords in Java?
Consider this code:
class Animal { void eat() { System.out.println("animal : eat"); }}class Dog extends Animal { void eat() { System.out.println("dog : eat"); } void anotherEat() { super.eat(); }}public class Test { public static void main(String[] args) { Animal a = new Animal(); a.eat(); Dog d = new Dog(); d.eat(); d.anotherEat(); }}
class Animal {
void eat() {
System.out.println("animal : eat");
}
class Dog extends Animal {
System.out.println("dog : eat");
void anotherEat() {
super.eat();
public class Test {
public static void main(String[] args) {
Animal a = new Animal();
a.eat();
Dog d = new Dog();
d.eat();
d.anotherEat();
The output is:
animal : eat
dog : eat
The third line is printing animal : eat because we are using super, if we have used this it would have printed dog : eat
31k questions
32.8k answers
501 comments
693 users