Unit 3-1
Unit 3-1
The Java API is a library of prewritten classes, that are free to use, included in the Java
Development Environment.
Syntax:
import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
Example
import java.util.Scanner;
In the example above, java.util is a package, while Scanner is a class of the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation
Example,
we will use the nextLine() method, which is used to read a complete line:
Example
Using the Scanner class to get user input:
import java.util.Scanner;
class MyClass
{
public static void main(String[] args)
{
Scanner myObj = new Scanner(System.in);
System.out.println("Enter username");
String userName = myObj.nextLine();
System.out.println("Username is: " + userName);
}
}
User-defined Packages:
User-defined packages are those packages that are designed or created by the developer
to categorize classes and packages. They are much similar to the built-in that java offers.
It can be imported into other classes and used the same as we use built-in packages.
But If we omit the package statement, the class names are put into the default package,
which has no name.
To create a package, we’re supposed to use the package keyword.
Syntax:
package package-name;
User-defined Packages (create your own packages)
Step 3: Compile and Run the Program
Step 1: Create the First Package
javac mypackage1/ClassA.java
package mypackage1; javac mypackage2/ClassB.java
public class ClassA
{
public void display()
{ Run the ClassB program:
System.out.println("Hello from ClassA in mypackage1!");
} java mypackage2.ClassB
}
// Getter methods
public String getName() {
return name;
}
java mainapp.Main
Interface in Java
An interface in Java is a blueprint of a class. It has static constants and abstract methods.
The interface in Java is a mechanism to achieve abstraction.
An interface is a kind of class. The interface contains methods and variables.
Syntax:
Interface interfacename
{
variables declaration;
methods declaration;
}
Example:
interface Animal
{
void sound();
void sleep()
{
System.out.println("The animal is sleeping");
}
}
Extending interface
Like class, interfaces can also extended. An interface can be sub interfaced from other interface.
Syntax:
Interface interfacename2 extends interfacename1
{
………..
……
}
Syntax: Example:
Class classname implements interfacename // Defining an interface
{ interface Animal {
Body of the class // Abstract method (does not have a body)
} void sound();
// Default method
default void sleep() {
System.out.println("The animal is sleeping");
}
}
// Calling methods
myDog.sound();
myDog.sleep();
myCat.sound();
myCat.sleep();
}
}
Exception Handling:
The Exception Handling in Java is one of the powerful mechanism to handle the runtime errors so that the normal
flow of the application can be maintained.
•Error: An Error indicates a serious problem that a reasonable application should not try to catch.
•Exception: Exception indicates conditions that a reasonable application might try to catch.
Java try and catch
The try statement allows you to define a block of code to be tested for errors while it is being executed.
The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.
The try and catch keywords come in pairs:
The finally statement lets you execute code, after try...catch, regardless of the result: