pidgeon.fly()
where fly()
is part of an interface or abstract class. The method’s implementation is not visible to the user, but it’s clear what it will do.Pidgeon extends Bird
Pidgeon
where they’re needed.
private
.public
getter/setter methods for accessing variables.private
.public
, private
, default (package-protected), protected
What is it?
Benefits
Ways to implement polymorphism
Example
// Parent Class
class Animal {
// Default implementation
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class Pig extends Animal {
// Overriding Animal.animalSound()
@Override
public void animalSound() {
System.out.println("The pig says: wee wee");
}
}
class MyMainClass {
public static void main(String[] args) {
Animal myAnimal = new Animal(); // Create a Animal object
Animal myPig = new Pig(); // Create a Pig object
myAnimal.animalSound(); // Base method
myPig.animalSound(); // Unique implementation method for related class
myDog.animalSound();
}
}