This is a bit hard to understand, but I think you'll be okay.
As always, let's start with these articles:
superclass- the parent
subclass- the child
Inheritance: borrowing what's there from the parent class.
We use the keyword extends to extend a class.
public class Sub_class extends Super_class {
Read the "IS-A Relationship" in the first article.
public class Animal {
}
public class Mammal extends Animal {
}
public class Reptile extends Animal {
}
public class Dog extends Mammal {
}
Think about these as:
Mammal is an animal. Reptile is an animal. A dog is a mammal.
Interface is sort of like extending, but simpler.
The interface is an outline of what sub classes must have. The class that implements the interface should fill out the info. Implements is the keyword.
public interface Vehicle {
// all are the abstract methods.
void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}
public class Bicycle implements Vehicle{
@Override
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
}
In WPILIB library, you will find that many classes inherit classes and implement interfaces.
You can see the tree of classes here.
Inheritance!
If say, a method requires a SendableBase,
public void doSth(SendableBase x)
and you have RobotDriveBase in your hand, you know that you can plug it in there with no problem, because ...... remember IS-A Relationship? RobotDriveBase IS A SendableBase.
Created: Dexin, 12/20/2018
Updated: Dexin, 12/20/2018