B.
Sc (Computer Science) – Java Unit 3 Notes
1. Final Keyword
Definition / Theory:
The final keyword in Java is a non-access modifier used to restrict users from modifying certain
elements of a program. It can be applied to variables, methods, and classes.
- A final variable’s value cannot be changed once initialized.
- A final method cannot be overridden in a subclass.
- A final class cannot be inherited by another class.
Explanation:
The final keyword helps achieve immutability and security in Java programs. It ensures that
important data and behavior remain constant and cannot be altered during program execution.
Example:
final class Vehicle {
final int speedLimit = 100;
final void display() {
[Link]("Speed Limit is " + speedLimit);
}
}
2. Abstract Class and Abstract Methods
Definition / Theory:
An abstract class in Java is declared using the keyword abstract and cannot be instantiated. An
abstract method is a method without a body that must be implemented by its subclass.
Explanation:
Abstract classes define a common template for other classes. They help achieve abstraction and
polymorphism by forcing subclasses to implement certain methods, while still allowing common
methods to be defined.
Example:
abstract class Shape {
abstract void draw();
void display() { [Link]("Shape class method"); }
}
class Circle extends Shape {
void draw() { [Link]("Drawing a Circle"); }
}
3. Interfaces
Definition / Theory:
An interface is a collection of abstract methods. It defines a contract that classes must follow and is
declared using the interface keyword.
Explanation:
Interfaces help achieve multiple inheritance and loose coupling in Java. They are used when
different classes need to share common behavior without being related by inheritance.
Example:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { [Link]("Bark"); }
}
4. System Packages and User-Defined Packages
Definition / Theory:
A package is a group of related classes, interfaces, and sub-packages. There are two types:
1. System (built-in) packages – provided by Java (e.g., [Link], [Link]).
2. User-defined packages – created by programmers.
Explanation:
Packages avoid naming conflicts, improve code organization, and enhance reusability. They also
control access levels using access modifiers.
Example:
package mypackage;
public class Greeting {
public void message() { [Link]("Hello from package!"); }
}
import [Link];
class Test {
public static void main(String args[]) {
Greeting g = new Greeting();
[Link]();
}
}
5. Try, Catch Block and Finally Clause
Definition / Theory:
The try-catch-finally structure handles exceptions in Java.
- The try block contains risky code.
- The catch block handles exceptions.
- The finally block executes code regardless of the outcome.
Explanation:
It ensures smooth program execution without abrupt termination. The finally block is used to close
resources or perform cleanup actions even when exceptions occur.
Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
[Link]("Error: " + e);
} finally {
[Link]("Finally executed");
}
6. User-Defined Exceptions
Definition / Theory:
A user-defined exception is a custom exception created by extending the Exception class. It is
used to handle application-specific errors.
Explanation:
When built-in exceptions don’t represent a specific condition, programmers create their own
exceptions to make the program more readable and meaningful.
Example:
class AgeException extends Exception {
AgeException(String msg) { super(msg); }
}
class Test {
public static void main(String args[]) {
try {
int age = 16;
if(age < 18)
throw new AgeException("Not eligible to vote");
} catch (AgeException e) {
[Link]("Caught: " + [Link]());
}
}
}