0% found this document useful (0 votes)
4 views8 pages

Some Imp Questions

The document explains the 'throws' keyword in Java, which indicates that a method may throw checked exceptions that need to be declared. It also outlines the differences between checked and unchecked exceptions, provides examples of each, and discusses features of Swing, wrapper classes, and extending interfaces in Java. Key concepts include exception handling, lightweight components, and the utility of wrapper classes for type conversion.

Uploaded by

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

Some Imp Questions

The document explains the 'throws' keyword in Java, which indicates that a method may throw checked exceptions that need to be declared. It also outlines the differences between checked and unchecked exceptions, provides examples of each, and discusses features of Swing, wrapper classes, and extending interfaces in Java. Key concepts include exception handling, lightweight components, and the utility of wrapper classes for type conversion.

Uploaded by

atharvanichat7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

throws keyword

 The throws keyword is used to indicate that a method might throw one or
more exceptions during its execution.
 Only checked exceptions (like IOException, SQLException) need to be
declared using throws.
 Unchecked exceptions (like NullPointerException, ArithmeticException)
don't require throws.

Syntax:

returnType methodName(parameters) throws ExceptionType1, ExceptionType2


{
// method body
}

import java.io.*;

public class Example


{
public static void readFile(String filename) throws IOException
{
FileReader fr = new FileReader(filename); // might throw IOException
}

public static void main(String[] args)


{
try
{
readFile("data.txt");
}
catch (IOException e)
{
System.out.println("File not found or cannot be read.");
}
}
}
Note Checked Exception ka matlab Compile Time Exception

Aur UnChecked Exception ka matlab Run Time Exception


Built In Exceptions

Checked Exception

These are checked at compile time. If not handled, the code will not compile.

Exception Class Description

IOException General I/O failures (e.g., file not found)

FileNotFoundException File does not exist

ClassNotFoundException Class not found during runtime

SQLException Database access errors

InterruptedException Thread interrupted while sleeping or waiting

ParseException Error in parsing text (e.g., date format)

InvocationTargetException Exception thrown by invoked method

NoSuchMethodException Method not found via reflection

Attempt to clone an object that does not implement


CloneNotSupportedException
Cloneable

Trying to create an instance of an abstract class or


InstantiationException
interface
Unchecked Exceptions

These are not checked at compile time. They extend RuntimeException.

Exception Class Description

NullPointerException Accessing an object with a null reference

ArrayIndexOutOfBoundsException Array index is out of bounds

Illegal arithmetic operation (e.g., divide by


ArithmeticException
zero)

Method receives illegal or inappropriate


IllegalArgumentException
argument

Method invoked at illegal or inappropriate


IllegalStateException
time

ClassCastException Invalid type casting

NumberFormatException Invalid string to number conversion

StringIndexOutOfBoundsException String index out of range

UnsupportedOperationException Operation not supported


Features of Swing in Java:

1. Lightweight components
2. Pluggable Look and Feel
3. Follows MVC architecture
4. Highly customizable
5. Event-driven programming
6. Pure Java implementation (platform-independent)
7. Integration with AWT
8. Supports drag and drop, tooltips, and accessibility

Wrapper Class

Key Features of Wrapper Classes:

1. Convert primitives to objects (Boxing)


2. Convert objects to primitives (Unboxing)
3. Useful in collections (e.g., ArrayList<Integer>)
4. Provide utility methods (e.g., parseInt(), toString())
5. Support null values (primitives cannot be null)
6. Immutable (once created, value cannot change)
7. Part of Java’s type system (autoboxing/autounboxing supported from Java 5)
8. Enable type conversion and parsing of strings

List of Wrapper Classes:

Primitive Type Wrapper Class

byte Byte

short Short

int Integer
Primitive Type Wrapper Class

long Long

float Float

double Double

char Character

boolean Boolean
Extending an interface

Extending an interface means creating a new interface that inherits the methods
of one or more existing interfaces.

This is similar to class inheritance, but interfaces can extend multiple interfaces,
supporting multiple inheritance of type.

Syntax:

java
CopyEdit
interface A
{
void methodA();
}

interface B extends A
{
void methodB();
}

 An interface can extend one or more interfaces using commas.


 The extending interface inherits all abstract methods from the parent
interfaces.
 The interface that extends others does not implement the methods—it just
inherits them.
 A class implementing the child interface must implement all inherited
methods.

Example:

interface Printable
{
void print();
}
interface Scannable
{
void scan();
}

interface MultiFunctionDevice extends Printable, Scannable


{
void copy();
}

class Machine implements MultiFunctionDevice


{
public void print()
{
System.out.println("Printing...");
}

public void scan()


{
System.out.println("Scanning...");
}

public void copy()


{
System.out.println("Copying...");
}
}

Jaise hum class ko extend karte hai waise hi interface ko extend karte hai.,…bus
aakhri me function ki coding class me karni padegi hame….interface nahi kar sakte
coding function ki

You might also like