Overview:
In Lesson 3, we delve into the powerful concepts of inheritance and polymorphism in Java. Inheritance allows one class to inherit the properties and behaviors of another, promoting code reuse. Polymorphism enables objects to take multiple forms, allowing flexibility and adaptability in the code.
Key Concepts:
- Extending Classes (Inheritance):
- Definition: Inheritance is a mechanism that allows a new class (subclass/derived class) to inherit properties and behaviors from an existing class (superclass/base class).
- Purpose: It promotes code reuse, extensibility, and the creation of a hierarchy of classes.
// Example of Inheritance
public class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
}
public class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking.");
}
}
Overloading Methods:
- Definition: Overloading involves defining multiple methods in the same class with the same name but different parameters (number, type, or order).
- Purpose: Overloading provides flexibility by allowing methods to handle different input scenarios.
// Example of Method Overloading
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
Overriding Methods:
- Definition: Overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.
- Purpose: Overriding allows a subclass to customize or extend the behavior of inherited methods.
// Example of Method Overriding
public class Shape {
public void draw() {
System.out.println("Drawing a shape.");
}
}
public class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
}
Example:
Let’s create a program that demonstrates inheritance, method overloading, and method overriding. We’ll define a base class Vehicle
, create a subclass Car
that extends Vehicle
, and showcase method overloading and overriding.
// Base class (superclass)
public class Vehicle {
public void start() {
System.out.println("Vehicle is starting.");
}
public void stop() {
System.out.println("Vehicle is stopping.");
}
}
// Subclass (derived class)
public class Car extends Vehicle {
// Method Overriding
@Override
public void start() {
System.out.println("Car is starting.");
}
// Method Overloading
public void accelerate() {
System.out.println("Car is accelerating.");
}
public void accelerate(int speed) {
System.out.println("Car is accelerating at " + speed + " km/h.");
}
}
// Main class
public class InheritanceExample {
public static void main(String[] args) {
// Creating objects
Vehicle vehicle = new Vehicle();
Car myCar = new Car();
// Using inherited methods
vehicle.start(); // Output: Vehicle is starting.
myCar.start(); // Output: Car is starting.
vehicle.stop(); // Output: Vehicle is stopping.
myCar.stop(); // Output: Vehicle is stopping. (Inherited method)
// Using overridden method
myCar.accelerate(); // Output: Car is accelerating.
// Using overloaded method
myCar.accelerate(80); // Output: Car is accelerating at 80 km/h.
}
}
In this example, Vehicle
is the superclass, and Car
is the subclass that extends Vehicle
. The Car
class overrides the start
method and overloads the accelerate
method. The InheritanceExample
class demonstrates how to create objects, invoke methods, and observe the effects of inheritance, method overriding, and method overloading.