[Go to site: main page, start]

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

? Java Polymorphism Programs ??

The document contains Java programs demonstrating polymorphism through various class hierarchies, including Animal and Cat, Vehicle and Car, Shape and Rectangle, and others. Each example illustrates the creation of a parent class with methods that are overridden by subclasses to provide specific functionality. The document serves as a practical guide for understanding object-oriented programming concepts in Java.

Uploaded by

Soumik Karmakar
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)
4 views30 pages

? Java Polymorphism Programs ??

The document contains Java programs demonstrating polymorphism through various class hierarchies, including Animal and Cat, Vehicle and Car, Shape and Rectangle, and others. Each example illustrates the creation of a parent class with methods that are overridden by subclasses to provide specific functionality. The document serves as a practical guide for understanding object-oriented programming concepts in Java.

Uploaded by

Soumik Karmakar
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

🧬 Java Polymorphism Programs 🚀🔥

🐾 1. Animal and Cat Class


❓ Question
📝 Write a Java program to create a class called Animal with a method called makeSound().​
🐱 Create a subclass called Cat that overrides the makeSound() method.
💻 Code
// Parent class

class Animal {

// Method to make sound

void makeSound() {

[Link]("Animal makes a sound");

// Child class inheriting Animal

class Cat extends Animal {

// Overriding makeSound() method

@Override

Soumik Karmakar - Object Oriented Programming with Java


void makeSound() {

[Link]("Cat meows");

// Main class

public class Main {

public static void main(String[] args) {

// Creating object of Cat class

Cat c = new Cat();

// Calling overridden method

[Link]();

🚗 2. Vehicle and Car Class


❓ Question
📝 Write a Java program to create a class called Vehicle with a method called drive().​
🚘 Create a subclass called Car that overrides the drive() method to print "Repairing a
car".

💻 Code
Soumik Karmakar - Object Oriented Programming with Java
// Parent class

class Vehicle {

// Method to drive

void drive() {

[Link]("Driving a vehicle");

// Child class

class Car extends Vehicle {

// Overriding drive() method

@Override

void drive() {

[Link]("Repairing a car");

// Main class

public class Main {

public static void main(String[] args) {

// Creating object of Car

Soumik Karmakar - Object Oriented Programming with Java


Car c = new Car();

// Calling overridden method

[Link]();

📐 3. Shape and Rectangle Class


❓ Question
📝 Write a Java program to create a class called Shape with a method called getArea().​
📏 Create a subclass called Rectangle that overrides the getArea() method to calculate
the area of a rectangle.

💻 Code
// Parent class

class Shape {

// Method for area

void getArea() {

[Link]("Calculating area");

// Child class

Soumik Karmakar - Object Oriented Programming with Java


class Rectangle extends Shape {

int length;

int width;

// Constructor

Rectangle(int length, int width) {

[Link] = length;

[Link] = width;

// Overriding getArea() method

@Override

void getArea() {

// Calculating area

int area = length * width;

[Link]("Area of Rectangle = " + area);

// Main class

public class Main {

Soumik Karmakar - Object Oriented Programming with Java


public static void main(String[] args) {

// Creating Rectangle object

Rectangle r = new Rectangle(10, 5);

// Calling overridden method

[Link]();

👨‍💼 4. Employee and HRManager Class


❓ Question
📝 Write a Java program to create a class called Employee with methods called work() and
getSalary().​
🏢 Create a subclass called HRManager that overrides the work() method and adds a new
method called addEmployee().

💻 Code
// Parent class

class Employee {

// Method for working

void work() {

[Link]("Employee is working");

Soumik Karmakar - Object Oriented Programming with Java


}

// Method for salary

void getSalary() {

[Link]("Salary = 50000");

// Child class

class HRManager extends Employee {

// Overriding work() method

@Override

void work() {

[Link]("HR Manager manages employees");

// New method

void addEmployee() {

[Link]("New employee added");

// Main class

Soumik Karmakar - Object Oriented Programming with Java


public class Main {

public static void main(String[] args) {

// Creating object

HRManager hr = new HRManager();

// Calling methods

[Link]();

[Link]();

[Link]();

🏦 5. BankAccount and SavingsAccount


Class
❓ Question
📝 Write a Java program to create a class called BankAccount with methods called
deposit() and withdraw().​
💰 Create a subclass called SavingsAccount that overrides the withdraw() method to
prevent withdrawals if the balance falls below 100.

💻 Code
// Parent class

class BankAccount {

Soumik Karmakar - Object Oriented Programming with Java


double balance = 1000;

// Deposit method

void deposit(double amount) {

balance += amount;

[Link]("Deposited: " + amount);

[Link]("Balance: " + balance);

// Withdraw method

void withdraw(double amount) {

balance -= amount;

[Link]("Withdrawn: " + amount);

[Link]("Balance: " + balance);

// Child class

class SavingsAccount extends BankAccount {

Soumik Karmakar - Object Oriented Programming with Java


// Overriding withdraw() method

@Override

void withdraw(double amount) {

// Checking minimum balance condition

if ((balance - amount) < 100) {

[Link]("Withdrawal denied!");

} else {

balance -= amount;

[Link]("Withdrawn: " + amount);

[Link]("Remaining Balance: " + balance);

// Main class

public class Main {

public static void main(String[] args) {

// Creating object

Soumik Karmakar - Object Oriented Programming with Java


SavingsAccount sa = new SavingsAccount();

// Depositing amount

[Link](500);

// Withdrawing amount

[Link](1300);

🐆 6. Animal and Cheetah Class


❓ Question
📝 Write a Java program to create a class called Animal with a method named move().​
⚡ Create a subclass called Cheetah that overrides the move() method to run.
💻 Code
// Parent class

class Animal {

// Method to move

void move() {

[Link]("Animal moves");

Soumik Karmakar - Object Oriented Programming with Java


// Child class

class Cheetah extends Animal {

// Overriding move() method

@Override

void move() {

[Link]("Cheetah runs very fast");

// Main class

public class Main {

public static void main(String[] args) {

// Creating object

Cheetah c = new Cheetah();

// Calling method

[Link]();

Soumik Karmakar - Object Oriented Programming with Java


👨‍🔧 7. Person and Employee Class
❓ Question
📝 Write a Java program to create a class called Person with methods called
getFirstName() and getLastName().​
🆔 Create a subclass called Employee that adds a new method named getEmployeeId()
and overrides the getLastName() method.

💻 Code
// Parent class

class Person {

String firstName;

String lastName;

// Constructor

Person(String firstName, String lastName) {

[Link] = firstName;

[Link] = lastName;

// Method to get first name

String getFirstName() {

return firstName;

Soumik Karmakar - Object Oriented Programming with Java


// Method to get last name

String getLastName() {

return lastName;

// Child class

class Employee extends Person {

int employeeId;

String jobTitle;

// Constructor

Employee(String firstName, String lastName,

int employeeId, String jobTitle) {

super(firstName, lastName);

[Link] = employeeId;

[Link] = jobTitle;

// Method to get employee ID

int getEmployeeId() {

Soumik Karmakar - Object Oriented Programming with Java


return employeeId;

// Overriding getLastName() method

@Override

String getLastName() {

return lastName + ", " + jobTitle;

// Main class

public class Main {

public static void main(String[] args) {

// Creating Employee object

Employee emp = new Employee(

"John",

"Smith",

101,

"Manager"

);

// Printing details

[Link]("First Name: " + [Link]());

Soumik Karmakar - Object Oriented Programming with Java


[Link]("Last Name: " + [Link]());

[Link]("Employee ID: " + [Link]());

⚪ 8. Shape and Circle Class


❓ Question
📝 Write a Java program to create a class called Shape with methods called getPerimeter()
and getArea().​
⭕ Create a subclass called Circle that overrides both methods.
💻 Code
// Parent class

class Shape {

// Method for perimeter

void getPerimeter() {

[Link]("Calculating perimeter");

// Method for area

void getArea() {

[Link]("Calculating area");

Soumik Karmakar - Object Oriented Programming with Java


}

// Child class

class Circle extends Shape {

double radius;

// Constructor

Circle(double radius) {

[Link] = radius;

// Overriding getPerimeter()

@Override

void getPerimeter() {

double perimeter = 2 * [Link] * radius;

[Link]("Perimeter = " + perimeter);

// Overriding getArea()

@Override

void getArea() {

Soumik Karmakar - Object Oriented Programming with Java


double area = [Link] * radius * radius;

[Link]("Area = " + area);

// Main class

public class Main {

public static void main(String[] args) {

// Creating object

Circle c = new Circle(7);

// Calling methods

[Link]();

[Link]();

🚛 9. Vehicle Class Hierarchy


❓ Question

Soumik Karmakar - Object Oriented Programming with Java


📝 Write a Java program to create a vehicle class hierarchy with subclasses Truck, Car, and
Motorcycle.​
⛽ Implement methods for fuel efficiency, distance traveled, and maximum speed.
💻 Code
// Base class

class Vehicle {

String make;

String model;

int year;

String fuelType;

// Constructor

Vehicle(String make, String model,

int year, String fuelType) {

[Link] = make;

[Link] = model;

[Link] = year;

[Link] = fuelType;

// Method for fuel efficiency

double fuelEfficiency() {

return 0;

Soumik Karmakar - Object Oriented Programming with Java


}

// Method for distance traveled

double distanceTravelled() {

return 0;

// Method for max speed

double maxSpeed() {

return 0;

// Truck class

class Truck extends Vehicle {

Truck(String make, String model,

int year, String fuelType) {

super(make, model, year, fuelType);

@Override

double fuelEfficiency() {

Soumik Karmakar - Object Oriented Programming with Java


return 8;

@Override

double distanceTravelled() {

return 500;

@Override

double maxSpeed() {

return 120;

// Car class

class Car extends Vehicle {

Car(String make, String model,

int year, String fuelType) {

super(make, model, year, fuelType);

@Override

Soumik Karmakar - Object Oriented Programming with Java


double fuelEfficiency() {

return 18;

@Override

double distanceTravelled() {

return 700;

@Override

double maxSpeed() {

return 180;

// Motorcycle class

class Motorcycle extends Vehicle {

Motorcycle(String make, String model,

int year, String fuelType) {

super(make, model, year, fuelType);

Soumik Karmakar - Object Oriented Programming with Java


@Override

double fuelEfficiency() {

return 35;

@Override

double distanceTravelled() {

return 400;

@Override

double maxSpeed() {

return 150;

// Main class

public class Main {

public static void main(String[] args) {

// Creating objects

Truck truck = new Truck("Tata", "TruckX", 2020, "Diesel");

Car car = new Car("Honda", "City", 2022, "Petrol");

Soumik Karmakar - Object Oriented Programming with Java


Motorcycle bike = new Motorcycle(

"Yamaha",

"R15",

2021,

"Petrol"

);

// Printing details

[Link]("Truck Mileage: "

+ [Link]());

[Link]("Car Max Speed: "

+ [Link]());

[Link]("Bike Distance: "

+ [Link]());

🏢 10. Employee Class Hierarchy


❓ Question

Soumik Karmakar - Object Oriented Programming with Java


📝 Write a Java program that creates a class hierarchy for employees of a company.​
👨‍💻 Create subclasses Manager, Developer, and Programmer with methods for bonus
calculation and performance reports.

💻 Code
// Base class

class Employee {

String name;

String address;

double salary;

String jobTitle;

// Constructor

Employee(String name, String address,

double salary, String jobTitle) {

[Link] = name;

[Link] = address;

[Link] = salary;

[Link] = jobTitle;

// Method for bonus

double calculateBonus() {

return 0;

Soumik Karmakar - Object Oriented Programming with Java


}

// Method for performance report

void performanceReport() {

[Link]("Performance report generated");

// Method for managing project

void manageProject() {

[Link]("Managing projects");

// Manager class

class Manager extends Employee {

Manager(String name, String address,

double salary, String jobTitle) {

super(name, address, salary, jobTitle);

@Override

double calculateBonus() {

Soumik Karmakar - Object Oriented Programming with Java


return salary * 0.20;

@Override

void manageProject() {

[Link]("Manager handles projects");

// Developer class

class Developer extends Employee {

Developer(String name, String address,

double salary, String jobTitle) {

super(name, address, salary, jobTitle);

@Override

double calculateBonus() {

return salary * 0.15;

@Override

Soumik Karmakar - Object Oriented Programming with Java


void performanceReport() {

[Link]("Developer performance is excellent");

// Programmer class

class Programmer extends Employee {

Programmer(String name, String address,

double salary, String jobTitle) {

super(name, address, salary, jobTitle);

@Override

double calculateBonus() {

return salary * 0.10;

@Override

void performanceReport() {

[Link]("Programmer completed coding tasks");

Soumik Karmakar - Object Oriented Programming with Java


// Main class

public class Main {

public static void main(String[] args) {

// Creating objects

Manager m = new Manager(

"Rahul",

"Delhi",

80000,

"Manager"

);

Developer d = new Developer(

"Amit",

"Mumbai",

60000,

"Developer"

);

Programmer p = new Programmer(

"Ravi",

"Pune",

50000,

Soumik Karmakar - Object Oriented Programming with Java


"Programmer"

);

// Displaying outputs

[Link]("Manager Bonus: "

+ [Link]());

[Link]();

[Link]();

Soumik Karmakar - Object Oriented Programming with Java

You might also like