Java Programming
Java Programming
Interfaces
An interface is a completely "abstract class" that is used to group related methods with
empty bodies:
// interface
interface Animal {
} To access the interface methods, the interface must be "implemented" (kinda like
inherited) by another class with the implements keyword (instead of extends). The body
of the interface method is provided by the "implement" class: Like abstract classes,
interfaces cannot be used to create objects (in the example above, it is not possible to
create an "Animal" object in the MyMainClass)
Interface methods do not have a body - the body is provided by the "implement" class
1) To achieve security - hide certain details and only show the important details of an
object (interface).
2) Java does not support "multiple inheritance" (a class can only inherit from one
superclass). However, it can be achieved with interfaces, because the class can
implement multiple interfaces. Note: To implement multiple interfaces, separate them
with a comma (see example below).
Example
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
Example
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
String Concatenation
use the concat() method to concatenate two strings:
Example
String firstName = "John ";
String lastName = "Doe";
System.out.println(firstName.concat(lastName));
isEmpty()
Determines whether or not a string is empty.
lastIndexOf()
In a string, this function returns the position of the last discovered
occurrence of a provided character.
replace() looks for a specified value in a string and returns a new
string with the provided values replaced.
Syntax:
java
Copy
Edit
public static void display() {
System.out.println("Static method");
}
2. Final Methods
A method declared with the final keyword cannot be overridden by
subclasses.
Syntax:
java
Copy
Edit
public final void show() {
System.out.println("Final method");
Helps in achieving security and consistency.
1. try Block
The try block is used to wrap code that might throw an exception.
Hierarchy of Exceptions:
php
Copy
Edit
Throwable
├── Exception
│ ├── Checked Exceptions
│ └── Unchecked Exceptions (RuntimeException)
└── Error (not meant to be handled by programs)
Types of Exceptions in Java:
1. Checked Exceptions
These are compile-time exceptions.
Examples:
IOException
SQLException
FileNotFoundException
Example:
java
Copy
Edit
FileReader file = new FileReader("data.txt"); // Might throw
FileNotFoundException
2. Unchecked Exceptions
These are runtime exceptions.
They are not checked at compile time.
Examples:
ArithmeticException
NullPointerException
ArrayIndexOutOfBoundsException
Example:
java
Copy
Edit
int a = 10 / 0; // Throws ArithmeticException
3. Errors
These are serious issues that are not meant to be caught in the
program.
Examples:
OutOfMemoryError
StackOverflowError..
4. Layout Manager
Layout Manager in Java is used to control the
arrangement of components (like buttons, text fields) in
a container.
It manages size and position automatically.
FlowLayout
BorderLayout
GridLayout
BoxLayout
GridBagLayout
The code must contain a main() method as the entry point for
execution.
4. Bytecode Verification
The Bytecode Verifier checks the loaded bytecode for security
and ensures it adheres to Java rules (e.g., no illegal access to
memory). 5. Execution (Using JVM)
The Java Virtual Machine (JVM) interprets or JIT-compiles the
bytecode into machine code for the underlying OS.
Example In pdf
Common Classes:
Common Classes: