BTECAD24405:: Technology Skill Dev.
-2 (Java)
Assignment No. – 6
Name- Nidhi Haridas
Roll no. – 28
Division-C
Batch –C2
Class - SY
Branch -AIML
Course Teacher Name - Prof. Mayuri Satpute
Assignment 6:
Write a Java program in which you will declare an abstract class Vehicle inherits this class from two
classes car and truck using the method engine in both displays “car has good engine” and “truck
has bad engine”..
CODE:
// Abstract Class
abstract class Vehicle {
// Abstract Method
abstract void engine();
// Car Class inheriting Vehicle
class Car extends Vehicle {
void engine() {
[Link]("Car has good engine");
// Truck Class inheriting Vehicle
class Truck extends Vehicle {
void engine() {
[Link]("Truck has bad engine");
}
// Main Class
public class Main {
public static void main(String[] args) {
Car c = new Car();
Truck t = new Truck();
[Link]();
[Link]();
}
Output:
Conclusion:
This program demonstrates the use of an abstract class in Java. The abstract class
Vehicle defines the abstract method engine(), which is implemented differently by the
Car and Truck classes. This shows how inheritance and abstraction allow different
classes to provide their own implementation of a common method.