Virtual Methods Flashcards

1
Q

Define a virtual method !

A

A virtual method is any method that can be overridden so all non static, non private and non final methods are virtual methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give a characteristic that makes a virtual method special !

A

What makes a virtual method special is that even if the call to the method on an object that overrides the method is made using a parent reference variable or made from within the parent class, you’ll always get the overridden version of the method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give an example that illustrates that !

A

public class Bird {
public String getName() {
return “Unknown”;
}
public void displayInformation() {
System.out.println(“The bird name is: “+getName());
}
}

public class Peacock extends Bird {
public String getName() {
return “Peacock”;
}
public static void main(String[] args) {
Bird bird = new Peacock();
bird.displayInformation();
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly