Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vehicle{
- private String brand;
- private int year;
- public Vehicle(String brand,int year){
- this.brand = brand;
- this.year = year;
- }
- public void displayInfo(){
- System.out.println("Brand: "+brand);
- System.out.println("Year: "+year);
- }
- public String getType(){
- return "Generic Vehicle";
- }
- }
- class Car extends Vehicle{
- private int numDoor;
- public Car(String brand,int year,int numDoor){
- super(brand,year);
- this.numDoor = numDoor;
- }
- public void displayInfo(){
- super.displayInfo();
- System.out.println("Number of Doors: "+numDoor);
- }
- public String getType(){
- return "Car";
- }
- }
- class Truck extends Vehicle{
- private double loadCapacity;
- public Truck(String brand,int year,double loadCapacity){
- super(brand,year);
- this.loadCapacity = loadCapacity;
- }
- public void displayInfo(){
- super.displayInfo();
- System.out.println("Load Capacity: "+loadCapacity+" Ton");
- }
- public String getType(){
- return "Truck";
- }
- }
- public class VehicleHierarchy{
- public static void main(String[] args){
- Vehicle car = new Car("Honda",1986,4);
- Vehicle truck = new Truck("Benz",1999,20);
- System.out.println("Vehicle Type: "+car.getType());
- car.displayInfo();
- System.out.print("\n");
- System.out.println("Vehicle Type: "+truck.getType());
- truck.displayInfo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement