Example 1: Simple Array

public class SimpleArray {

    public static void main(String[] args) {

        // Declare and initialize an array of integers

        int[] numbers = {1, 2, 3, 4, 5};


        // Display the elements of the array

        System.out.print("Array Elements: ");

        for (int number : numbers) {

            System.out.print(number + " ");

        }

    }

}

Output:

Array Elements: 1 2 3 4 5


Example 2: Array Sum

public class ArraySum {

    public static void main(String[] args) {

        // Declare and initialize an array of integers

        int[] numbers = {10, 20, 30, 40, 50};


        // Calculate the sum of array elements

        int sum = 0;

        for (int number : numbers) {

            sum += number;

        }


        // Display the sum

        System.out.println("Sum of Array Elements: " + sum);

    }

}

Output:

Sum of Array Elements: 150


Example 3: Inheritance - Superclass and Subclass

class Animal {

    void sound() {

        System.out.println("Some generic animal sound");

    }

}


class Dog extends Animal {

    void bark() {

        System.out.println("Woof! Woof!");

    }

}


public class InheritanceExample {

    public static void main(String[] args) {

        // Create an object of the subclass

        Dog myDog = new Dog();


        // Call methods from both the superclass and subclass

        myDog.sound();

        myDog.bark();

    }

}

Output:

Some generic animal sound

Woof! Woof!


Example 4: Array of Objects

class Student {

    String name;


    Student(String n) {

        name = n;

    }

}


public class ArrayOfObjects {

    public static void main(String[] args) {

        // Declare and initialize an array of Student objects

        Student[] students = {

                new Student("John"),

                new Student("Jane"),

                new Student("Bob")

        };


        // Display the names of students

        System.out.print("Student Names: ");

        for (Student student : students) {

            System.out.print(student.name + " ");

        }

    }

}

Output:

Student Names: John Jane Bob


Example 5: Inheritance - Overriding Method

class Shape {

    void draw() {

        System.out.println("Drawing a shape");

    }

}


class Circle extends Shape {

    @Override

    void draw() {

        System.out.println("Drawing a circle");

    }

}


public class OverridingExample {

    public static void main(String[] args) {

        // Create objects of both the superclass and subclass

        Shape shape = new Shape();

        Circle circle = new Circle();


        // Call the overridden method

        shape.draw();  // Output: Drawing a shape

        circle.draw();  // Output: Drawing a circle

    }

}

Output:

Drawing a shape

Drawing a circle


Example 6: Array of Superclass Objects

class Vehicle {

    void start() {

        System.out.println("Vehicle starting...");

    }

}


class Car extends Vehicle {

    @Override

    void start() {

        System.out.println("Car starting...");

    }

}


public class ArrayOfSuperclassObjects {

    public static void main(String[] args) {

        // Declare and initialize an array of Vehicle objects

        Vehicle[] vehicles = {

                new Vehicle(),

                new Car(),

                new Vehicle()

        };


        // Call the overridden method

        for (Vehicle vehicle : vehicles) {

            vehicle.start();

        }

    }

}

Output:

Vehicle starting...

Car starting...

Vehicle starting...


If you have any question then comment below - We will answer your questions. And do not forget to follow