1. Write a Java program that demonstrates method overloading. Create a class Calculator with overloaded methods to add both integers and floating-point numbers?
class Calculator { public int add(int a, int b) { return a + b; } public float add(float a, float b) { return a + b; } public float add(int a, float b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); System.out.println(“Sum of 5 and 10 (int): ” + calc.add(5, 10)); System.out.println(“Sum of 5.5 and 10.5 (float): ” + calc.add(5.5f, 10.5f)); System.out.println(“Sum of 5 and 10.5 (int + float): ” + calc.add(5, 10.5f)); } } |
Output :
Sum of 5 and 10 (int): 15 Sum of 5.5 and 10.5 (float): 16.0 Sum of 5 and 10.5 (int + float): 15.5 |
2. Write a program to create a superclass Animal with a method makeSound(). Derive two classes Dog and Cat from Animal and override the makeSound() method. Demonstrate polymorphism by calling the makeSound() method on a reference of type Animal?
class Animal { public void makeSound() { System.out.println(“Animal makes a sound”); } } class Dog extends Animal { public void makeSound() { System.out.println(“Dog barks”); } } class Cat extends Animal { public void makeSound() { System.out.println(“Cat meows”); } } public class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); Animal myDog = new Dog(); Animal myCat = new Cat(); myAnimal.makeSound(); myDog.makeSound(); myCat.makeSound(); } } |
Output :
Animal makes a sound Dog barks Cat meows |
3. Write a program to define an interface Shape with a method draw(). Implement the Shape interface in two classes, Circle and Rectangle, and demonstrate polymorphism by calling the draw() method using a Shape reference?
interface Shape { void draw(); } class Circle implements Shape { public void draw() { System.out.println(“Drawing a Circle”); } } class Rectangle implements Shape { public void draw() { System.out.println(“Drawing a Rectangle”); } } public class Main { public static void main(String[] args) { Shape myShape1 = new Circle(); Shape myShape2 = new Rectangle(); myShape1.draw(); myShape2.draw(); } } |
Output :
Drawing a Circle Drawing a Rectangle |
4. Write a program to create a superclass Shape with a method area(). Derive two classes Circle and Rectangle from Shape and override the area() method to calculate the respective areas. Demonstrate polymorphism by calling the area() method on a Shape reference?
class Shape { public double area() { return 0; } } class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; // Area = π * r² } } class Rectangle extends Shape { private double length, width; // Constructor for Rectangle public Rectangle(double length, double width) { this.length = length; this.width = width; } public double area() { return length * width; // Area = length * width } } public class Main { public static void main(String[] args) { Shape myCircle = new Circle(5); Shape myRectangle = new Rectangle(4, 6); System.out.println(“Area of Circle: ” + myCircle.area()); System.out.println(“Area of Rectangle: ” + myRectangle.area()); } } |
Output :
Area of Circle: 78.53981633974483 Area of Rectangle: 24.0 |
5. Write a program to create an interface Animal with a method sound(). Implement this interface in classes Dog and Cat. Create a List<Animal>, add instances of Dog and Cat, and call the sound() method on each element in the list?
import java.util.ArrayList; import java.util.List; interface Animal { void sound(); } class Dog implements Animal { @Override public void sound() { System.out.println(“Dog barks”); } } class Cat implements Animal { @Override public void sound() { System.out.println(“Cat meows”); } } public class Main { public static void main(String[] args) { List<Animal> animals = new ArrayList<>(); animals.add(new Dog()); animals.add(new Cat()); for (Animal animal : animals) { animal.sound(); } } } |
Output :
Dog barks Cat meows |
6. Write a program to create a superclass Parent with a static method display(). Create a subclass Child that also defines a static method display()?
class Parent { public static void display() { System.out.println(“Display method in Parent class”); } } class Child extends Parent { public static void display() { System.out.println(“Display method in Child class”); } } public class Main { public static void main(String[] args) { Parent.display(); Child.display(); Parent parent = new Child(); parent.display(); } } |
Output :
Display method in Parent class Display method in Child class Display method in Parent class |
7. Write a program to create a base class Parent with a static method showMessage(). Then, create a derived class Child that hides the showMessage() method. Demonstrate how static methods are not polymorphic by calling showMessage() on a reference of type Parent and Child?
class Parent { public static void showMessage() { System.out.println(“Message from Parent class”); } } class Child extends Parent { public static void showMessage() { System.out.println(“Message from Child class”); } } public class Main { public static void main(String[] args) { Parent parent = new Parent(); parent.showMessage(); Child child = new Child(); child.showMessage(); Parent polyReference = new Child(); polyReference.showMessage(); } } |
Output :
Message from Parent class Message from Child class Message from Parent class |
8. Write a program to create an interface Playable with a method play(). Create multiple classes Guitar, Drums, and Piano that implement Playable. Demonstrate polymorphism by storing these objects in an array or list and invoking the play() method on each object?
import java.util.ArrayList; import java.util.List; interface Playable { void play(); } class Guitar implements Playable { @Override public void play() { System.out.println(“Playing the Guitar”); } } class Drums implements Playable { @Override public void play() { System.out.println(“Playing the Drums”); } } class Piano implements Playable { @Override public void play() { System.out.println(“Playing the Piano”); } } public class Main { public static void main(String[] args) { List<Playable> instruments = new ArrayList<>(); instruments.add(new Guitar()); instruments.add(new Drums()); instruments.add(new Piano()); for (Playable instrument : instruments) { instrument.play(); } } } |
Output :
Playing the Guitar Playing the Drums Playing the Piano |