Method Overriding in Java
Method overriding in Java is a feature of object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This means that the subclass method replaces the implementation of the same method in the superclass.
To perform method overriding in Java, you need to use the @Override annotation with the derived class method, and the method in the base class should be non-static and accessible (e.g., public or protected).
Why Use Method Overriding?
- Flexibility and Specialization: Derived classes can adapt inherited methods to their specific needs, promoting code reusability and polymorphism (the ability of objects to respond differently to the same message).
- Extending Functionality: You can add new behavior to inherited methods while preserving the core functionality from the base class.
- Elegant Design: Method overriding fosters well-structured and maintainable code by promoting code reuse and clear inheritance hierarchies.
Scenario:
Imagine you are creating a virtual animal simulation. In this simulation, different animals can make sounds. There is a general sound that any animal might make, but specific animals like dogs have their unique sounds. You will create a program to represent this using inheritance and method overriding.
Java Program Example:
class Animal {
public void bark() {
System.out.println(“The animal makes a generic sound.”);
}
}
class Dog extends Animal {
@Override
public void bark() {
System.out.println(“The dog barks: Woof! Woof!”);
}
}
public class Program {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.bark();
}
}
Output:
The dog barks: Woof! Woof!
Example :
class Animal
// Defines a new class named Animal.
public void bark()
// Declares a method bark in the Animal class. This method can be overridden in derived classes.
System.out.println(“The animal makes a generic sound.”);
// Prints the message to the console.
class Dog extends Animal
// Defines a new class named Dog that inherits from the Animal class by using the “extends” keyword.
@Override
public void bark()
// Overrides the bark method in the Animal class. The @Override annotation indicates this method replaces the base class method.
System.out.println(“The dog barks: Woof! Woof!”);
// Prints the message to the console.
public class Program
// Defines a new class named Program.
public static void main(String[] args) {
Dog myDog = new Dog();
// Creates a new instance of the Dog class.
myDog.bark();
// Calls the bark method on the Dog instance. This will output the dog-specific barking sound.
Tasks:
1. Basic Overriding:
Write a base class Shape with a method draw(). Create a derived class Circle that overrides the draw() method to print “Drawing Circle”.
2. Polymorphism with Overriding:
Define a base class Animal with a method makeSound(). Create derived classes Dog and Cat that override makeSound() to print “Bark” and “Meow” respectively. Demonstrate polymorphism by calling makeSound() on a list of Animal objects.
3. Calling Base Class Method:
Create a base class Vehicle with a method describe(). In a derived class Car, override describe() but also call the base class describe() method within the overridden method.
4. Abstract Methods:
Write an abstract class Employee with an abstract method calculateSalary(). Create derived classes FullTimeEmployee and PartTimeEmployee that implement the calculateSalary() method.
5. Overriding Properties:
Define a base class Person with a method getName() and setName(String name). Create a derived class Student that overrides these methods to add custom behavior when setting and getting the name property.
6. Overriding toString() Method:
Write a class Book that overrides the toString() method to return a string representation of the book’s title and author.
7. Sealed Method:
Create a base class BaseLogger with a method log(). In a derived class FileLogger, override the log() method and mark it as final. Then create another derived class DetailedFileLogger and demonstrate that log() cannot be overridden further.
Course Video
YouTube Reference :
The training covers Java method overriding, including its concepts, syntax, and practical applications.
Yes, it includes examples demonstrating how to override methods in subclasses to provide specific implementations.
Yes, exercises involve creating base and derived classes, implementing method overriding, and understanding its usage in various scenarios.
Yes, it’s designed for beginners with clear explanations and step-by-step examples.
A Java IDE like Eclipse, IntelliJ IDEA, or an online compiler will suffice.
Yes, it covers how method overriding allows subclasses to provide specific implementations, enhancing flexibility and code reusability.
Yes, the training is self-paced and accessible online anytime.
Yes, it explains the use of the @Override
annotation to indicate that a method is intended to override a method in a superclass.
It typically takes a few hours to complete, depending on your learning pace.
Yes, this Java Method Overriding training is completely free on Iqra Technology Academy’s website.