UNIT II – OOP, PACKAGES, INTERFACES & EXCEPTION HANDLING (Java)
(Based on Herbert Schildt – Java: The Complete Reference)
1. Method Overloading
Definition
Method overloading is a feature in Java that allows multiple methods with the same name
but different parameter lists within the same class.
It provides compile-time polymorphism.
Syntax
returnType methodName(type1 arg1)
returnType methodName(type1 arg1, type2 arg2)
Explanation
Methods differ by number, type, or order of parameters
Return type alone cannot differentiate overloaded methods
Resolved at compile time
Program
class Overload {
void add(int a, int b) {
[Link](a + b);
}
void add(double a, double b) {
[Link](a + b);
}
public static void main(String args[]) {
Overload obj = new Overload();
[Link](10, 20);
[Link](5.5, 6.5);
}
}
Input
10 20
5.5 6.5
Output
30
12.0
2. Method Overriding
Definition
Method overriding allows a subclass to provide a specific implementation of a method
already defined in its superclass.
It supports runtime polymorphism.
Syntax
class Parent {
void show() { }
}
class Child extends Parent {
void show() { }
}
Explanation
Same method name, parameters, and return type
Access level cannot be reduced
Resolved at runtime
Program
class Parent {
void display() {
[Link]("Parent class method");
}
}
class Child extends Parent {
void display() {
[Link]("Child class method");
}
public static void main(String args[]) {
Child obj = new Child();
[Link]();
}
}
Output
Child class method
3. Abstract Classes
Definition
An abstract class is a class declared using the abstract keyword that may contain abstract
and non-abstract methods.
It cannot be instantiated.
Syntax
abstract class ClassName {
abstract void method();
}
Explanation
Used to achieve partial abstraction
Can have constructors and instance variables
Subclass must implement abstract methods
Program
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
[Link]("Drawing Circle");
}
public static void main(String args[]) {
Shape s = new Circle();
[Link]();
}
}
Output
Drawing Circle
4. Dynamic Method Dispatch
Definition
Dynamic method dispatch is the mechanism by which a call to an overridden method is
resolved at runtime, based on the object being referenced.
Explanation
Achieved using method overriding
Base class reference can refer to subclass object
Program
class A {
void call() {
[Link]("Inside A");
}
}
class B extends A {
void call() {
[Link]("Inside B");
}
public static void main(String args[]) {
A obj = new B();
[Link]();
}
}
Output
Inside B
5. Usage of final Keyword
Definition
The final keyword is used to restrict modification.
Uses
Final variable → constant
Final method → cannot be overridden
Final class → cannot be inherited
Program
final class Demo {
final int x = 10;
final void show() {
[Link](x);
}
public static void main(String args[]) {
Demo d = new Demo();
[Link]();
}
}
Output
10
6. Packages
Definition
A package is a group of related classes and interfaces.
Syntax
package packname;
Program
package mypack;
public class Sample {
public void msg() {
[Link]("Package Example");
}
}
Output
Package Example
7. Access Protection
Access Specifiers
Modifier Scope
private Same class
default Same package
protected Package + subclass
public Everywhere
8. Importing Packages
Syntax
import [Link];
Program
import [Link];
class Test {
public static void main(String args[]) {
Sample s = new Sample();
[Link]();
}
}
Output
Package Example
9. Interfaces
Definition
An interface is a collection of abstract methods used to achieve full abstraction.
Syntax
interface InterfaceName {
void method();
}
10. Implementing Interfaces
Program
interface Printable {
void print();
}
class Printer implements Printable {
public void print() {
[Link]("Printing...");
}
public static void main(String args[]) {
Printable p = new Printer();
[Link]();
}
}
Output
Printing...
11. Extending Interfaces
Program
interface A {
void show();
}
interface B extends A {
void display();
}
class Demo implements B {
public void show() {
[Link]("Show method");
}
public void display() {
[Link]("Display method");
}
public static void main(String args[]) {
Demo d = new Demo();
[Link]();
[Link]();
}
}
Output
Show method
Display method
PART B – EXCEPTION HANDLING
12. Exception Handling
Definition
Exception handling is a mechanism to handle runtime errors, preventing abnormal
termination of the program.
13. try–catch Block
Syntax
try {
// risky code
}
catch(Exception e) {
// handling code
}
Program
class TryCatchDemo {
public static void main(String args[]) {
try {
int a = 10 / 0;
}
catch(ArithmeticException e) {
[Link]("Cannot divide by zero");
}
}
}
Output
Cannot divide by zero
14. throw Keyword
Program
class ThrowDemo {
static void checkAge(int age) {
if(age < 18)
throw new ArithmeticException("Not eligible");
else
[Link]("Eligible");
}
public static void main(String args[]) {
checkAge(16);
}
}
Output
Exception in thread main [Link]: Not eligible
15. throws Keyword
Program
class ThrowsDemo {
static void read() throws Exception {
throw new Exception("Error occurred");
}
public static void main(String args[]) throws Exception {
read();
}
}
Output
Exception in thread main [Link]: Error occurred
16. finally Block
Definition
The finally block always executes whether an exception occurs or not.
Program
class FinallyDemo {
public static void main(String args[]) {
try {
int x = 10 / 2;
}
catch(Exception e) {
[Link](e);
}
finally {
[Link]("Finally block executed");
}
}
}
Output
Finally block executed
17. Built-in Exceptions
Examples:
ArithmeticException
NullPointerException
ArrayIndexOutOfBoundsException
IOException
18. User-Defined Exception
Program
class MyException extends Exception {
MyException(String msg) {
super(msg);
}
}
class UserExDemo {
static void validate(int marks) throws MyException {
if(marks < 40)
throw new MyException("Fail");
else
[Link]("Pass");
}
public static void main(String args[]) throws MyException {
validate(35);
}
}
Output
Exception in thread main MyException: Fail