Polymorphism Abstraction&Interfaces
Polymorphism Abstraction&Interfaces
if (isPalindrome(input)) {
System.out.println(input + " is a Palindrome");
} else {
System.out.println(input + " is not a Palindrome");
}
}
/*This program prompts the user to input a string, converts it to lowercase, and
then checks if it is a palindrome using the isPalindrome method. If the input
string is the same forwards and backwards (ignoring case), it is considered a
palindrome.
2: You need to find and print all the unique characters in a given string.
Output : jv
ans:
*/
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/*1:This program prompts the user to input a string, iterates through each
character, and adds it to a HashSet. Since a HashSet does not allow duplicate
elements, only unique characters will be added. Finally, it prints out the unique
characters found in the input string.
java
*/
// Superclass
class HillStations {
// Method to get location
void location() {
System.out.println("Location: Hill Station");
}
// Subclass Manali
class Manali extends HillStations {
// Overriding location method
void location() {
System.out.println("Location: Manali");
}
// Subclass Mussoorie
class Mussoorie extends HillStations {
// Overriding location method
void location() {
System.out.println("Location: Mussoorie");
}
// Subclass Gulmarg
class Gulmarg extends HillStations {
// Overriding location method
void location() {
System.out.println("Location: Gulmarg");
}
mussoorie.location();
mussoorie.famousFor();
System.out.println();
gulmarg.location();
gulmarg.famousFor();
}
}
ans:: */
// Superclass Animal
class Animal {
// Method to make sound
void makeSound() {
System.out.println("The animal makes a sound.");
}
}
// Subclass Dog
class Dog extends Animal {
// Override makeSound method
@Override
void makeSound() {
System.out.println("The dog barks.");
}
}
// Subclass Cat
class Cat extends Animal {
// Override makeSound method
@Override
void makeSound() {
System.out.println("The cat meows.");
}
}
System.out.print("Dog: ");
dog.makeSound();
System.out.print("Cat: ");
cat.makeSound();
}
}
/*[ The Animal class serves as the superclass with a method makeSound() that
prints "The animal makes a sound."
The Dog and Cat classes are subclasses of Animal. They override the makeSound()
method to print "The dog barks." and "The cat meows." respectively.
In the main method, objects of each class are created, and the makeSound()
method of each object is called to display the sound each animal makes.]
ans:
*/
// Abstract class Vaccine
abstract class Vaccine {
// Variables
int age;
String nationality;
// Constructor
public Vaccine(int age, String nationality) {
this.age = age;
this.nationality = nationality;
}
/*note[ Vaccine is an abstract class with variables age and nationality, and
concrete methods firstDose() and secondDose().
The VaccinationSuccessful class extends Vaccine and implements the abstract
method boosterDose().
In the main method of the Vaccination class, an object of the
VaccinationSuccessful class is created and its methods are invoked according to the
scenarios provided.]
*/
/*Manali, Mussoorie, Gulmarg. Subclasses extend the superclass and override its
location() and famousFor() method.
i.call the location() and famousFor() method by the Parent class’, i.e.
Hillstations class. As it refers to the base class object and the base class method
overrides the superclass method; the base class method is invoked at runtime.
ii.call the location() and famousFor() method by the all subclass’,and print
accordingly.
// Superclass HillStations
class HillStations {
// Method to get location
void location() {
System.out.println("Location: Hill Station");
}
// Subclass Manali
class Manali extends HillStations {
// Override method for location
@Override
void location() {
System.out.println("Location: Manali, Himachal Pradesh");
}
// Subclass Mussoorie
class Mussoorie extends HillStations {
// Override method for location
@Override
void location() {
System.out.println("Location: Mussoorie, Uttarakhand");
}
// Subclass Gulmarg
class Gulmarg extends HillStations {
// Override method for location
@Override
void location() {
System.out.println("Location: Gulmarg, Jammu and Kashmir");
}
mussoorie.location();
mussoorie.famousFor();
gulmarg.location();
gulmarg.famousFor();
}
}
output::
/*
Write a Java program that demonstrates method overriding by creating a superclass
called Animal and two subclasses called Dog and Cat.
● The Animal class should have a method called makeSound(), which simply prints
"The animal makes a sound."
● The Dog and Cat classes should override this method to print "TheCat/The dog
meows/barks" respectively.
● The program should allow the user to create and display objects of each class.
*/
/*Here's the Java program demonstrating method overriding using superclass Animal
and subclasses Dog and Cat:*/
// Superclass Animal
class Animal {
// Method to make sound
void makeSound() {
System.out.println("The animal makes a sound.");
}
}
// Subclass Dog
class Dog extends Animal {
// Override method for makeSound
@Override
void makeSound() {
System.out.println("The dog barks.");
}
}
// Subclass Cat
class Cat extends Animal {
// Override method for makeSound
@Override
void makeSound() {
System.out.println("The cat meows.");
}
}
System.out.print("Dog: ");
dog.makeSound();
System.out.print("Cat: ");
cat.makeSound();
}
}
OUTPUT:
Animal: The animal makes a sound.
Dog: The dog barks.
Cat: The cat meows.
/* Animal is the superclass with a method makeSound() which prints "The animal
makes a sound."
Subclasses Dog and Cat override the makeSound() method to provide specific
sounds for each animal.
In the main method, objects of each class are created and their makeSound()
methods are called to demonstrate method overriding.
*/