Modify inherited behaviors and access parent functionality using the super keyword.
What if you could inherit your parents' car but upgrade the engine and change the paint job without building a whole new vehicle from scratch?
In Object-Oriented Programming, inheritance allows a subclass to take on the properties of a superclass. However, sometimes the inherited behavior isn't quite right for the child. Method Overriding occurs when a subclass provides a specific implementation for a method that is already defined in its parent class. To override a method, the child's version must have the exact same name, return type, and parameter list as the parent's version. This allows for polymorphism, where a single method call can behave differently depending on the object type. Think of a generic `Shape` class with a `draw()` method; a `Circle` subclass would override `draw()` to render a curve, while a `Square` would override it to render straight lines.
1. Create a parent class `Animal` with a method `makeSound()`. 2. Create a child class `Dog` that extends `Animal`. 3. In `Dog`, write a method `public void makeSound()` that prints 'Bark'. 4. When you call `myDog.makeSound()`, the program ignores the generic animal sound and uses the specific 'Bark' implementation.
Quick Check
What three things must be identical between the parent method and the child method for an override to occur?
Answer
The method name, the return type, and the parameter list (the method signature).
Sometimes you don't want to completely replace a parent's behavior—you just want to add to it. The super keyword is a reference variable used to refer to the immediate parent class object. You can use it to call a parent's version of a method before adding new logic. More importantly, `super()` is used to call the parent's constructor. Since a child object 'contains' a parent object, the parent must be initialized first. If you don't explicitly call `super()`, the compiler often tries to insert a hidden call to the parent's default constructor automatically. Using `super` ensures that we maintain the 'DRY' (Don't Repeat Yourself) principle by leveraging existing code.
1. Parent class `Employee` has a constructor that sets `name` and `id`. 2. Child class `Manager` adds a `department` field. 3. In the `Manager` constructor, the first line is `super(name, id);`. 4. This passes the data up to the `Employee` constructor to handle the shared fields, while the `Manager` constructor only handles the `department` field.
Quick Check
Where must the 'super()' call be placed inside a subclass constructor?
Answer
It must be the very first statement in the constructor.
It is easy to confuse Overriding with Overloading, but they serve different purposes. Overloading happens within the same class (or across inheritance) when two methods have the same name but different parameters. It is a form of compile-time polymorphism. Overriding happens between a parent and child class where the methods have the same parameters.
Let be the parent method and be the child method. - If , it is Overriding. - If but , it is Overloading.
1. A `BankAccount` class has a method `withdraw(double amount)`. 2. A `SavingsAccount` overrides `withdraw` to check if the balance stays above $100. 3. Inside the overridden method, it uses `if (balance - amount >= 100)`, then calls `super.withdraw(amount)` to actually perform the deduction. 4. This combines specialized validation with the original logic of the parent class.
Which keyword is used to call a method from the parent class that has been overridden?
If a child class has a method with the same name as the parent but different parameters, what is this called?
A subclass constructor will always call its parent's constructor, even if 'super()' is not explicitly written.
Review Tomorrow
In 24 hours, try to explain the difference between 'super()' and 'super.methodName()' to a peer.
Practice Activity
Create a 'SmartPhone' class that extends a 'CellPhone' class. Override the 'sendText' method to include an automatic signature at the end of every message.