0% found this document useful (0 votes)
0 views2 pages

Lab4 Inheritance

The document outlines a Java program demonstrating inheritance through a superclass 'Vehicle' and subclasses 'Car' and 'SportsCar'. It includes methods for driving, displaying details, and showcases upcasting and dynamic method dispatch. The main class 'InheritanceDemo' tests the functionality of these classes with various vehicle instances.

Uploaded by

sfhoun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views2 pages

Lab4 Inheritance

The document outlines a Java program demonstrating inheritance through a superclass 'Vehicle' and subclasses 'Car' and 'SportsCar'. It includes methods for driving, displaying details, and showcases upcasting and dynamic method dispatch. The main class 'InheritanceDemo' tests the functionality of these classes with various vehicle instances.

Uploaded by

sfhoun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

// Step 1: Create a superclass

class Vehicle {
protected String make;
private int year;

public Vehicle(String make, int year) {


this.make = make;
this.year = year;
}

public void drive() {


System.out.println("Vehicle is being driven");
}

public void drive(String destination) {


System.out.println("Vehicle is driving to the destination: " + destination);
}

public void drive(int distance) {


System.out.println("Vehicle is driving for " + distance + " miles");
}

public final void printMake() {


System.out.println("Make: " + make);
}

public void displayYear() {


System.out.println("Vehicle year is: " + year);
}
}

// Step 2: Create a subclass with simple inheritance


class Car extends Vehicle {
private String model;

public Car(String make, String model, int year) {


super(make, year);
this.model = model;
}

public void displayDetails() {


System.out.println("Car Make: " + make + ", Model: " + model);
}

@Override
public void drive() {
System.out.println("Car is being driven");
}

public void displayYear() {


super.displayYear();
System.out.println("Car year is: " + super.make);
}
}

// Step 3: Create a subclass with multilevel inheritance


class SportsCar extends Car {
public SportsCar(String make, String model, int year) {
super(make, model, year);
}

@Override
public void drive() {
System.out.println("Sports car is being driven");
}
}

// Step 10: Run and test the program


public class InheritanceDemo {
public static void main(String[] args) {
Vehicle v = new Vehicle("Generic", 2020);
v.drive();
v.drive("Beach");
v.drive(100);

Car c = new Car("Toyota", "Camry", 2021);


c.drive();
c.displayDetails();
c.displayYear();

SportsCar sc = new SportsCar("Ferrari", "488", 2022);


sc.drive();

// Step 6: Upcasting
Vehicle upcastedCar = new SportsCar("Lamborghini", "Huracan", 2023);
upcastedCar.drive();

// Step 7: Dynamic method dispatch


Vehicle[] vehicles = {new Car("Honda", "Civic", 2021), new SportsCar("Bugatti", "Chiron
for (Vehicle vehicle : vehicles) {
vehicle.drive();
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy