Java Program Create and Import a Package

 
package example;

public class MyClass {
    public void display() {
        System.out.println("Hello, jpwebdevelopers!");
    }
}
//import and use the MyClass from the example package.
// File: Main.java

import example.MyClass;

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.display();
    }
}

Java Program Creating a Package with Multiple Classes

 
package mypackage;

public class MyClass {
    public void displayMessage() {
        System.out.println("Hello from MyClass!");
    }
}

//AnotherClass.java

package mypackage;

public class AnotherClass {
    public void displayMessage() {
        System.out.println("Hello from AnotherClass!");
    }
}

//import and create a third Java file, "Main.java", outside the "mypackage" folder to demonstrate accessing these classes.
// File: Main.java

import mypackage.MyClass;
import mypackage.AnotherClass;

public class Main {
    public static void main(String[] args) {
        MyClass myObj = new MyClass();
        AnotherClass anotherObj = new AnotherClass();

        myObj.displayMessage();
        anotherObj.displayMessage();
    }
}

Java program that demonstrates the usage of some built-in packages.

 
import java.util.Arrays;
import java.util.Date;
import java.text.SimpleDateFormat;

public class Main {
    public static void main(String[] args) {
        // Arrays class from java.util package
        int[] numbers = {5, 2, 9, 1, 7};
        Arrays.sort(numbers);
        System.out.println("Sorted Numbers: " + Arrays.toString(numbers));

        // Date and SimpleDateFormat 
        //classes from java.util package
        Date currentDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        String formattedDate = dateFormat.format(currentDate);
        System.out.println("Current Date: " + formattedDate);
    }
}

Java program to Implementing an Interface

 

interface Greeting {
    void greet();
}

// Implement the interface in a class
class HelloWorld implements Greeting {
    public void greet() {
        System.out.println("Hello, jpwebdevelopers!");
    }
}

public class Main {
    public static void main(String[] args) {
       
        HelloWorld hello = new HelloWorld();
        
        // Call the greet() method 
        hello.greet();
    }
}

Java program to implement Multiple Inheritance using interface

 

interface Interface1 {
    void method1();
}

interface Interface2 {
    void method2();
}

// Implement interfaces in a class
class MyClass implements Interface1, Interface2 {
    public void method1() {
        System.out.println("jpwebdevelopers");
    }

    public void method2() {
        System.out.println("jpnotes");
    }
}

public class Main {
    public static void main(String[] args) {
     
        MyClass myObj = new MyClass();

        // Call the methods 
        myObj.method1();
        myObj.method2();
    }
}

Java program to implement Abstract class using interface.

 
// Define an interface
interface Animal {
    void sound();
}

abstract class AbstractAnimal implements Animal {
    abstract void eat();
}

class Dog extends AbstractAnimal {
    void sound() {
        System.out.println("Dog barks");
    }

    void eat() {
        System.out.println("Dog eats bones");
    }
}

public class Main {
    public static void main(String[] args) {
        // Create an object of the Dog class
        Dog dog = new Dog();

        // Call the methods 
        //defined in the Animal interface
        dog.sound();

        // Call the method defined in the
        // AbstractAnimal abstract class
        dog.eat();
    }
}


index