Lab 7
Lab 7
Lab no:08
METHOD OVERRIDING & POLYMORPHISM
1. Create a class Card, and its children classes are Valentine, Holiday, and Birthday. A card class will
have a greeting () method that writes out a greeting. Each type of card contains an appropriate greeting.
The Holiday card says "Season's Greetings." The Birthday card says "Happy Birthday." The Eid card says
"Happy blissful Eid". Create objects of the three child classes: Holiday, Birthday and Eid and show the
polymorphic behavior (call the greeting methods from each object).
Code: Output:
// Card.java
public class Card {
public void greeting() {
System.out.println("Greetings!");
}
}
// Holiday.java
public class Holiday extends Card {
public void greeting() {
System.out.println("Season's Greetings.");
}
}
// Birthday.java
public class Birthday extends Card {
public void greeting() {
System.out.println("Happy Birthday.");
}
}
// Eid.java
public class Eid extends Card {
public void greeting() {
System.out.println("Happy blissful Eid.");
}
NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B
}
// Main.java
public class Main {
public static void main(String[] args) {
Card holidayCard = new Holiday();
Card birthdayCard = new Birthday();
Card eidCard = new Eid();
holidayCard.greeting();
birthdayCard.greeting();
eidCard.greeting();
}
}
2. Create a super class called Car. The Car class has the following fields and methods. int speed;
double regularPrice; String color; double getSalePrice(); Create a sub class of Car class and name it as
Truck. The Truck class has the following fields and methods.int weight;
doublegetSalePrice();//Ifweight>2000,10%discount.Otherwise,20%disc ount. Create a subclass of Car
class and name it as Ford. The Ford class has the following fields and methods. int year; int
manufacturerDiscount; double getSalePrice();//From the sale price computed from Car class, subtract
the manufacturer Discount. Create MyOwnAutoShop class which contains the main() method.
Perform the following within the main() method. Create two instances of the Ford and Truck class and
initialize all the fields with appropriate values. Use super(...) method in the constructor for initializing
the fields of the super class. Create an instance of Car class and initialize all the fields with appropriate
values. Display the sale prices of all instance. Show polymorphic behavior.
Code: Output:
// Car.java
public class Car {
int speed;
double regularPrice;
String color;
this.color = color;
}
public double getSalePrice() {
return regularPrice;
}
}
// Truck.java
public class Truck extends Car {
int weight;
public Truck(int speed, double regularPrice, String color, int weight) {
super(speed, regularPrice, color);
this.weight = weight;
}
public double getSalePrice() {
if (weight > 2000) {
return super.getSalePrice() * 0.9; // 10% discount
} else {
return super.getSalePrice() * 0.8; // 20% discount
}
}
}
// Ford.java
public class Ford extends Car {
int year;
int manufacturerDiscount;
public Ford(int speed, double regularPrice, String color, int year, int manufacturerDiscount) {
super(speed, regularPrice, color);
this.year = year;
this.manufacturerDiscount = manufacturerDiscount;
}
NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B