Inheritance allows one class to inherit properties and methods from another class. This helps reduce code duplication and models real-world relationships.
For example, a Dog is an Animal, so Dog can inherit common behavior from Animal.
Use the extends keyword to create a subclass. The subclass automatically gets access to the parent class's public and protected members.
Once a class extends another, objects of the child class can use both its own methods and inherited methods from the parent class.
A subclass can override a method from the parent class to provide its own implementation. Use @Override for clarity and error checking.
The super keyword refers to the parent class. It is commonly used to call the parent constructor or access parent methods.
Java supports multi-level inheritance, where classes can extend other subclasses, forming a hierarchy.
Java allows a class to extend only one class (single inheritance). Constructors are not inherited, and private members cannot be accessed directly.
Create a Vehicle class and a Car subclass that overrides the start() method.