Page 189 - CITS - CSA - TP (Volume 2) - Hindi
P. 189
कं ूटर सॉ वेयर ए ीके शन - CITS
System.out.println(“This is a generic vehicle.”);
}
}
// Derived class (Subclass) extending Vehicle
class Car extends Vehicle {
// Override the displayInfo method from the Vehicle class
@Override
public void displayInfo() {
System.out.println(“This is a car.”);
}
// Additional method specific to Car
public void startEngine() {
System.out.println(“Car engine started.”);
}
}
// Main class to demonstrate method overriding
public class MethodOverrideBasicExample {
public static void main(String[] args) {
// Create an instance of the Car class
Car myCar = new Car();
// Call the overridden method
myCar.displayInfo(); // Outputs: This is a car.
// Call the additional method specific to Car
myCar.startEngine(); // Outputs: Car engine started.
}
}
ीकरण:
1 आधार ास (वाहन):
• इसम वाहन के बारे म सामा जानकारी दिश त करने के िलए displayInfo नामक िविध शािमल है।
2 ु ास (कार):
• वाहन ास का िव ार करता है।
• िकसी कार के िलए िविश काया यन दान करने के िलए displayInfo िविध को ओवरराइड करता है।
• कार ास के िलए िविश एक और िविध ाट इंजन जोड़ता है।
175
CITS : IT & ITES - कं ूटर सॉ वेयर ए ीके शन - अ ास 112

