Glossary
Constructor
A special method used to initialize new objects of a class. In inheritance, a subclass's constructor is responsible for calling its superclass's constructor to ensure proper initialization of inherited parts.
Example:
When creating a SportsCar object, its constructor might first call the Car superclass constructor to set basic car properties before adding sports-specific features.
Inheritance
An object-oriented programming principle where a new class (subclass) is created from an existing class (superclass), acquiring its attributes and behaviors. This promotes code reuse and establishes an 'is-a' relationship.
Example:
The concept of inheritance allows a Sedan class to reuse the startEngine() method defined in its Car superclass, avoiding redundant code.
No-argument constructor
A constructor that takes no parameters. If a subclass constructor doesn't explicitly call a superclass constructor using `super()`, Java automatically inserts an implicit call to the superclass's no-argument constructor.
Example:
If Dog extends Animal and Animal has a default no-argument constructor, creating a new Dog() will implicitly call Animal() first.
Object class
The root class of the Java class hierarchy. Every class in Java implicitly or explicitly extends the `Object` class, meaning all objects inherit its fundamental methods like `toString()` and `equals()`.
Example:
Even a simple HelloWorld class implicitly extends the Object class, giving it access to methods like toString() and equals().
Subclass
A class that inherits methods and instance variables from a superclass. It can add its own unique features or override inherited ones.
Example:
Dog is a subclass of Animal, inheriting behaviors like eat() and sleep(), but also having its own bark() method.
Superclass
A class whose methods and instance variables are inherited by another class. It serves as a general blueprint for its specialized subclasses.
Example:
In a game, Vehicle could be a superclass for Car and Motorcycle, defining common properties like speed and color.
super keyword
A keyword used within a subclass constructor to explicitly call a constructor of its immediate superclass. This ensures the superclass's instance variables are properly initialized before the subclass's specific initialization.
Example:
In a Square class constructor, **super**(side, side, side, side); might be used to call the Quadrilateral constructor, passing the side length for all four sides.