This Java program demonstrates the use of static methods in a class hierarchy. In this program, there are two classes: Vehicle (the parent class) and Car (the child class). Both classes have a static method named display. Static methods belong to the class rather than to instances of the class, and they can be called using the class name itself. Here's an explanation of the program:
This program demonstrates the use of static methods in a class hierarchy. Static methods belong to the class itself and can be called using the class name. Even though the Car class inherits from the Vehicle class, it can have its own static method with the same name, and the method invoked depends on the class in which it is called. In this case, the display method in the Car class hides the one in the Vehicle class when called using the class name.
public class StaticMethods // Main class { public static void main(String[] args) { Vehicle.display(); // Accessing static method in parent class Car.display(); // Accessing static method in child class } } class Vehicle // Parent class { static void display() { System.out.println("This is a Vehicle"); } } class Car extends Vehicle // Child class { static void display() { System.out.println("This is a Car"); } }
This is a Vehicle This is a Car
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions