1. Program for polymorphism.
class Animal {
void sound() {
System.out.println("The animal makes a sound");
class Dog extends Animal {
@Override
void sound() {
System.out.println("The dog barks");
class Cat extends Animal {
@Override
void sound() {
System.out.println("The cat meows");
public class Polymorphism {
public static void main(String[] args) {
Animal[] animals = new Animal[3];
animals[0] = new Dog();
animals[1] = new Cat();
animals[2] = new Animal();
for (Animal animal : animals) {
animal.sound();
}
2. Inheritance(4 types)
Use this and super keyword
class Animal {
void eat() {
System.out.println("Eating...");
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
class BabyDog extends Dog {
void weep() {
System.out.println("Weeping...");
public class Inheritance {
public static void main(String[] args) {
BabyDog bd = new BabyDog();
bd.eat();
bd.bark();
bd.weep();
bd.super.eat();
bd.super.bark();
((Dog)bd).bark();
((Animal)bd).eat();
class Animal {
Animal() {
System.out.println("Animal constructor");
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog constructor");
public class Inheritance {
public static void main(String[] args) {
Dog d = new Dog();
}
class Animal {
void display() {
System.out.println("Animal class");
class Dog extends Animal {
void display() {
super.display();
System.out.println("Dog class");
public class Inheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.display();
class Animal {
void eat() {
System.out.println("Animal eats");
}
class Dog extends Animal {
void eat() {
super.eat();
System.out.println("Dog eats");
public class Inheritance {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
3. Encapsulation
class Employee {
private int id;
private String name;
private double salary;
public Employee(int id, String name, double salary) {
(link unavailable) = id;
this.name = name;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
(link unavailable) = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
public class Encapsulation {
public static void main(String[] args) {
Employee e = new Employee(1, "John", 50000);
System.out.println(e.getId());
System.out.println(e.getName());
System.out.println(e.getSalary());
e.setId(2);
e.setName("Jane");
e.setSalary(60000);
System.out.println(e.getId());
System.out.println(e.getName());
System.out.println(e.getSalary());
}
}
interface Printable {
void print();
}
interface Showable {
void show();
}
class A implements Printable, Showable {
public void print() {
System.out.println("Printing from A");
}
public void show() {
System.out.println("Showing from A");
}
}
4. Interface
public class Interface {
public static void main(String[] args) {
A a = new A();
a.print();
a.show();
}
}
interface Shape {
void area();
}
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public void area() {
System.out.println("Area of circle: " + 3.14 * radius * radius);
}
}
class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public void area() {
System.out.println("Area of rectangle: " + length * width);
}
}
public class Interface {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 5);
circle.area();
rectangle.area();
}
}
5. Exception handling
public class ExceptionHandling {
public static void main(String[] args) {
try {
int a = 10/0;
} catch(ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
try {
int[] arr = new int[5];
System.out.println(arr[10]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of bounds");
}
try {
String str = null;
System.out.println(str.length());
} catch(NullPointerException e) {
System.out.println("String is null");
}
try {
int num = Integer.parseInt("abc");
} catch(NumberFormatException e) {
System.out.println("Invalid number format");
}
}
}
public class ExceptionHandling {
public static void main(String[] args) {
try {
throw new Exception("Custom exception");
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
public class ExceptionHandling {
public static void main(String[] args) {
try {
method1();
} catch(Exception e) {
System.out.println("Caught in main");
}
}
public static void method1() throws Exception {
try {
method2();
} catch(Exception e) {
System.out.println("Caught in method1");
throw e;
}
}
public static void method2() throws Exception {
throw new Exception("Exception from method2");
}
}
6. ArrayList Program:
import java.util.ArrayList;
public class ArrayListProgram {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
System.out.println(list);
list.remove(1);
System.out.println(list);
list.set(0, "Date");
System.out.println(list);
System.out.println(list.contains("Cherry"));
System.out.println(list.size());
}
}
HashSet Program:
import java.util.HashSet;
public class HashSetProgram {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("Apple");
set.add("Banana");
set.add("Cherry");
System.out.println(set);
set.remove("Banana");
System.out.println(set);
System.out.println(set.contains("Cherry"));
System.out.println(set.size());
}
}
ArrayList and HashSet Program:
import java.util.ArrayList;
import java.util.HashSet;
public class ArrayListHashSetProgram {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Cherry");
HashSet<String> set = new HashSet<>(list);
System.out.println(set);
list.clear();
System.out.println(list);
System.out.println(set);
}
}