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

Lab Task 002

The document contains multiple Java class implementations demonstrating inheritance and method overriding. Each section defines a base class and subclasses that extend its functionality, showcasing polymorphism through method calls. The main classes instantiate objects and invoke overridden methods to illustrate their behavior.

Uploaded by

alliyafatima78
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)
4 views4 pages

Lab Task 002

The document contains multiple Java class implementations demonstrating inheritance and method overriding. Each section defines a base class and subclasses that extend its functionality, showcasing polymorphism through method calls. The main classes instantiate objects and invoke overridden methods to illustrate their behavior.

Uploaded by

alliyafatima78
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

LAB TASK 02

Question 01:
class Vehicle{
public void move()
{
System.out.println("The vehicle is moving");
}
}
class Car extends Vehicle{
public void move()
{
super.move();
System.out.println("The car is moving");
}
}
class Bike extends Vehicle
{
public void move() {
super.move();
System.out.println("The bike is moving");
}
}
public class Main {
public static void main(String[] args)
{
Vehicle myCar =new Car();
Vehicle myBike =new Bike();
myCar.move();
myBike.move();
}
}

Output:
Question 02:
class Animal {
public void makeSound() {
System.out.println("Animals make sounds");
}
}

class Dog extends Animal {


public void makeSound() {
super.makeSound();
System.out.println("Dog barks");
}
}

class Cat extends Animal {


public void makeSound() {
super.makeSound();
System.out.println("Cat meows");
}
}

public class Mainn{


public static void main(String[] args) {
Animal[] animals = new Animal[2];
animals[0] = new Dog();
animals[1] = new Cat();
for (Animal animal : animals) {
animal.makeSound();
System.out.println();
}
}
}
Output:
Question 03:
class BankAccount {
void transaction() {
System.out.println("Performing a general transaction");
}
}

class SavingsAccount extends BankAccount {


public void transaction() {
super.transaction();
System.out.println("Performing savings account transaction");
}
}

class CurrentAccount extends BankAccount {


public void transaction() {
super.transaction();
System.out.println("Performing current account transaction");
}
}

public class AccountManagementSystem {


public static void main(String[] args) {
BankAccount savings = new SavingsAccount();
BankAccount current = new CurrentAccount();
savings.transaction();
current.transaction();
}
}

Output:

Question 04:
class Employee {
void calculateBonus() {
System.out.println("Calculating base employee bonus");
}
}

class Manager extends Employee {


public void calculateBonus() {
super.calculateBonus();
System.out.println("Calculating manager bonus");
}
}

class Developer extends Employee {


public void calculateBonus() {
super.calculateBonus();
System.out.println("Calculating developer bonus");
}
}

public class Management {


public static void main(String[] args) {
Employee manager = new Manager();
Employee developer = new Developer();
manager.calculateBonus();
developer.calculateBonus();
}
}

Output:

You might also like