Object-Oriented Programming
Reuse and organize code using object-oriented principles.
Object-oriented programming (OOP) helps organize large programs into reusable pieces.
Inheritance
A class can inherit from another class.
public class Animal {
public void speak() {
System.out.println("...");
}
}public class Dog extends Animal {
}Dog automatically has the speak() method.
Method Overriding
A subclass can replace a method from its parent.
@Override
public void speak() {
System.out.println("Woof!");
}Polymorphism
A parent reference can refer to different child objects.
Animal animal = new Dog();Calling speak() runs the Dog version.
Interfaces
An interface defines what a class must implement.
public interface Driveable {
void drive();
}Implement it:
public class Robot implements Driveable {
@Override
public void drive() {
}
}Many FTC libraries use interfaces to define common behavior.
Abstract Classes
An abstract class cannot be created directly.
It is meant to be extended by other classes.
public abstract class Subsystem {
public abstract void update();
}FTC Example
Many FTC classes inherit from other classes or implement interfaces. For example, OpModes extend LinearOpMode or OpMode.
Key Takeaways
- Inheritance lets classes reuse code.
- Polymorphism allows one type to represent many objects.
- Interfaces define required behavior.
- Abstract classes provide a common foundation.
Was this resource helpful?
