0% found this document useful (0 votes)
12 views10 pages

Java Methods, Access Specifiers & Exception Handling

The document provides an overview of key Java programming concepts including methods, access specifiers, static and final members, classes, objects, encapsulation, polymorphism, interfaces, and exception handling. It explains the structure of methods, the purpose of access specifiers, and the significance of static and final keywords. Additionally, it covers exception handling mechanisms and the types of exceptions in Java, emphasizing the importance of handling exceptions for program stability.

Uploaded by

vidya.2428it973
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Java Methods, Access Specifiers & Exception Handling

The document provides an overview of key Java programming concepts including methods, access specifiers, static and final members, classes, objects, encapsulation, polymorphism, interfaces, and exception handling. It explains the structure of methods, the purpose of access specifiers, and the significance of static and final keywords. Additionally, it covers exception handling mechanisms and the types of exceptions in Java, emphasizing the importance of handling exceptions for program stability.

Uploaded by

vidya.2428it973
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

📌 1.

Methods in Java

A method in Java is a block of code designed to perform a specific task.

Methods improve reusability, readability, and modularity.

General structure:

returnType methodName(parameters) {

// method body

Methods may or may not return a value and can accept parameters.

📌 2. Access Specifiers

Access specifiers define the visibility of classes, methods, and variables.

public – accessible from anywhere

private – accessible only within the same class

protected – accessible within the same package and subclasses

default – accessible only within the same package


They help implement encapsulation and control data hiding.

📌 3. Static Members

Static members belong to the class rather than an object.

They are shared among all objects of the class and can be accessed using the class name.

Example:

static int count;

static void display() { ... }

Static blocks execute once when the class is loaded.

📌 4. Final Members

The keyword final is used to declare constants and restrict modification.

Final variable: value cannot be changed

Final method: cannot be overridden

Final class: cannot be inherited

It ensures security and prevents accidental modification.


📌 5. Class

A class is a blueprint or template for creating objects.

It contains data members (variables) and member functions (methods).

Example:

class Student {

int id;

String name;

📌 6. Object

An object is an instance of a class.

It represents a real-world entity and provides memory to store the class variables.

Example:

Student s = new Student();

Objects access class methods and variables.

Encapsulation
means hiding the data of a class and allowing access only through methods.

Access to the data is provided through public getters and setters, ensuring data hiding,
security, and controlled modification.
ATM Machine

 अंदर balance hidden रहता है


 आप direct balance variable तक नहीं पहुंच सकते
 सिर्फ दिए हुए buttons (methods) से ही access कर सकते हैं

Polymorphism
is the ability of an object or method to take multiple forms. It allows the same method
name to perform different actions depending on the context. Java supports polymorphism
through method overloading (compile-time) and method overriding (runtime).

Why Polymorphism is Needed?

 Makes code flexible

 Reduces duplication

 Allows child classes to change parent behavio

🟦 TYPES OF POLYMORPHISM

1️⃣ Compile-Time Polymorphism (Method Overloading)

This is resolved by the compiler.

Same method name

 Different number/type of parameters


= Different behaviors
2️⃣ Runtime Polymorphism (Method Overriding)

This is resolved at execution time (runtime).

A child class can provide its own version of a method already defined in the parent class.

✔️Example:

Output: Dog Barks


Interface
in Java is a blueprint of a class that contains abstract methods (methods without
body).

 A class uses implements keyword.

 One class can implement multiple interfaces.

✔ Why use Interfaces?

 To achieve abstraction

 To support multiple inheritance

 To enforce common behavior across unrelated classes


⭐ Exception Handling (Very Easy Notes)

1. What is an Exception?

An exception is an error that happens while a program is running.


If we do not handle it, the program will stop.

Example:
10 / 0 gives an ArithmeticException.

⭐ 2. try
We write the risky code inside try block.

Example:

⭐ 3. catch
The catch block runs when an exception happens.
It is used to handle the error.

Example:
⭐ 4. finally
The finally block always runs, even if exception comes or not.
Used for cleanup work.

Example:

⭐ 5. throw
throw is used when we want to manually create an exception.

Example:

⭐ 6. throws
If a method may cause an exception, we write throws in its declaration.

Example:
⭐ 7. Types of Exceptions

A. Checked Exceptions (Compile-time)

 Java checks these exceptions before running the program.

 Must handle using try–catch or throws.

Examples:

 IOException

 FileNotFoundException

B. Unchecked Exceptions (Runtime)

 These exceptions come while program is running.

 Handling is optional.

Examples:

 ArithmeticException

 NullPointerException

 ArrayIndexOutOfBoundsException

⭐ 8. In-built Exceptions (Already given by Java)


Name When it occurs

ArithmeticException Divide by zero

NullPointerException Using null object

ArrayIndexOutOfBoundsException Wrong array index


Name When it occurs

NumberFormatException Wrong input number

⭐ 9. User-Defined Exception (Custom Exception)


We can make our own exception class.

Example:

⭐ 10. Why Exception Handling is


Important?
 Program does not crash

 We can give meaningful error messages

 Code becomes safe and easy to maintain

You might also like