Program illustrating classes and objects
class Car {
String brand;
String model;
int year;
Car(String brand, String model, int year) {
this.brand = brand;
this.model = model;
this.year = year;
}
void displayInfo() {
System.out.println(year + " " + brand + " " + model);
}
public static void main(String[] args) {
Car car1 = new Car("Toyota", "Corolla", 2020);
Car car2 = new Car("Honda", "Civic", 2022);
car1.displayInfo();
car2.displayInfo();
}
}
Output:
2020 Toyota Corolla
2022 Honda Civic
Program illustrating method overloading and method overriding
class MathOperations {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
class AdvancedMath extends MathOperations {
@Override
int add(int a, int b) {
return (a + b) * 2;
}
}
public class Main {
public static void main(String[] args) {
MathOperations math = new MathOperations();
System.out.println("Overloaded Method (2 args): " + math.add(5, 10));
System.out.println("Overloaded Method (3 args): " + math.add(5, 10, 15));
AdvancedMath advMath = new AdvancedMath();
System.out.println("Overridden Method: " + advMath.add(5, 10));
}
}
Output:
Overloaded Method (2 args): 15
Overloaded Method (3 args): 30
Overridden Method: 30
Program illustrating concept of interface
interface Animal {
void makeSound();
void eat();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("Dog barks");
}
public void eat() {
System.out.println("Dog eats bones");
}
}
class Cat implements Animal {
public void makeSound() {
System.out.println("Cat meows");
}
public void eat() {
System.out.println("Cat drinks milk");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound();
dog.eat();
cat.makeSound();
cat.eat();
}
}
Output:
Dog barks
Dog eats bones
Cat meows
Cat drinks milk
Program illustrating use of final and super keyword
class Animal {
String name = "Animal";
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
String name = "Dog";
void displayNames() {
System.out.println("Parent Name: " + super.name);
System.out.println("Child Name: " + name);
}
@Override
void makeSound() {
super.makeSound();
System.out.println("Dog barks");
}
final void sleep() {
System.out.println("Dog is sleeping");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.displayNames();
dog.makeSound();
dog.sleep();
}
}
Output:
Parent Name: Animal
Child Name: Dog
Animal makes a sound
Dog barks
Dog is sleeping
Program that illustrates the creation of a simple package
// Package file (mypackage/MyClass.java)
package mypackage;
public class MyClass {
public void displayMessage() {
System.out.println("Hello from MyClass in mypackage!");
}
}
// Main file (Main.java)
import mypackage.MyClass;
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.displayMessage();
}
}
Output:
Hello from MyClass in mypackage!
Program that illustrates handling of predefined exceptions
public class ExceptionHandlingExample {
public static void main(String[] args) {
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
}
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Array index out of bounds.");
}
try {
String str = null;
System.out.println(str.length());
} catch (NullPointerException e) {
System.out.println("Error: Null reference encountered.");
}
System.out.println("Program execution continues...");
}
}
Output:
Error: Division by zero is not allowed.
Error: Array index out of bounds.
Error: Null reference encountered.
Program execution continues...
Program that illustrates handling of user-defined exceptions
class InvalidAgeException extends Exception {
public InvalidAgeException(String message) {
super(message);
}
}
public class CustomExceptionExample {
static void validateAge(int age) throws InvalidAgeException {
if (age < 18) {
throw new InvalidAgeException("Age must be 18 or above to vote.");
} else {
System.out.println("You are eligible to vote.");
}
}
public static void main(String[] args) {
try {
validateAge(16);
} catch (InvalidAgeException e) {
System.out.println("Exception Caught: " + e.getMessage());
}
try {
validateAge(20);
} catch (InvalidAgeException e) {
System.out.println("Exception Caught: " + e.getMessage());
}
}
}
Output:
Exception Caught: Age must be 18 or above to vote.
You are eligible to vote.