0% found this document useful (0 votes)
46 views4 pages

Lab 7

The document describes creating classes for different types of cards that extend a base Card class. It shows overriding the greeting() method in subclasses to return customized greetings. It also describes creating classes for vehicles that extend a base Car class, with subclasses for Truck and Ford that override the getSalePrice() method. Objects are created for each class and their sale prices are displayed.

Uploaded by

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

Lab 7

The document describes creating classes for different types of cards that extend a base Card class. It shows overriding the greeting() method in subclasses to return customized greetings. It also describes creating classes for vehicles that extend a base Car class, with subclasses for Truck and Ford that override the getSalePrice() method. Objects are created for each class and their sale prices are displayed.

Uploaded by

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

NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B

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;

public Car(int speed, double regularPrice, String color)


{
this.speed = speed;
this.regularPrice = regularPrice;
NAME: Muhammad Shayan Ahmed ROLL NO: 088 SECTION: B

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

public double getSalePrice() {


return super.getSalePrice() - manufacturerDiscount;
}
}
// MyOwnAutoShop.java
public class MyOwnAutoShop {
public static void main(String[] args) {
Car ford1 = new Ford(120, 25000, "Blue", 2019, 2000);
Car ford2 = new Ford(140, 30000, "Red", 2020, 2500);
Car truck = new Truck(100, 35000, "Black", 3000);
Car car = new Car(160, 20000, "Green");
System.out.println("Sale price of Ford 1: $" + ford1.getSalePrice());
System.out.println("Sale price of Ford 2: $" + ford2.getSalePrice());
System.out.println("Sale price of Truck: $" + truck.getSalePrice());
System.out.println("Sale price of Car: $" + car.getSalePrice());
}
}

You might also like