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

JavaPaper2

The document explains the concept of default methods in Java interfaces, allowing interfaces to provide implementations without forcing all implementing classes to override them. It also covers inner classes implementing interfaces, the differences between processes and threads, and provides examples of creating threads and performing database operations using JDBC. Additionally, it includes sample outputs for the code snippets demonstrating these concepts.

Uploaded by

Tejas61
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)
2 views8 pages

JavaPaper2

The document explains the concept of default methods in Java interfaces, allowing interfaces to provide implementations without forcing all implementing classes to override them. It also covers inner classes implementing interfaces, the differences between processes and threads, and provides examples of creating threads and performing database operations using JDBC. Additionally, it includes sample outputs for the code snippets demonstrating these concepts.

Uploaded by

Tejas61
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
You are on page 1/ 8

Default Method in Interface (Java)

A default method is a method with a body (implementation) defined inside an


interface using the default keyword. This allows interfaces to provide backward
compatibility without forcing all implementing classes to override the
method.interfaces could only have abstract methods. If you wanted to add a new
method, all implementing classes had to update. Default methods solve this by
letting the interface provide a default implementation.

interface Animal {

void sound(); // Abstract method

// Default method

default void sleep() {

System.out.println("The animal is sleeping.");

class Dog implements Animal {

public void sound() {

System.out.println("Dog barks.");

public class Main {

public static void main(String[] args) {

Dog d = new Dog();

d.sound(); // Implemented in Dog

d.sleep(); // Inherited from interface (default)

}
}

OUTPUT :-

Dog barks.

The animal is sleeping

Java Program: Inner Class Implementing an Interface

// Interface

interface Greetable {

void greet();

// Outer class

class Outer {

String message = "Hello from Outer class";

// Inner class implementing the interface

class Inner implements Greetable {

public void greet() {

System.out.println("Greeting from Inner class!");

System.out.println("Accessing outer class message: "


+ message);

// Method to access inner class method


void showGreeting() {

Inner inner = new Inner(); // Creating object of inner


class

inner.greet(); // Calling method from inner class

// Main class to run the program

public class Main {

public static void main(String[] args) {

Outer outer = new Outer();

outer.showGreeting(); // Accessing inner class method


through outer class

Exception Hierarchy

Feature Process Thread


Definition A process is an instance of A thread is a lightweight
a program in execution. subunit of a process,
Feature Process Thread
which can run
concurrently with other
threads.
Threads share the same
Each process has its own
Resource memory space and
memory space and
Allocation resources of the process
resources.
they belong to.
Inter-process Threads can communicate
Communicat communication (IPC) is directly within the same
ion required for processes to process using shared
communicate. memory.
Thread creation is faster
Process creation is slower
and requires fewer
and requires more
Creation resources because
resources because it
Overhead threads share the
involves setting up its own
process’s memory and
memory and resources.
resources.
Threads are part of a
process and run
Processes are independent
Execution concurrently with other
and execute separately.
threads in the same
process.
Threads within the same
Processes are isolated
process are not isolated;
from each other,
Isolation one thread can affect
preventing one process
others within the same
from affecting another.
process.
Processes can run Threads run concurrently
concurrently, but each and can be managed by a
Concurrency
process runs in its own thread scheduler to utilize
memory space. multiple processors.
If a process crashes, it If a thread crashes, it may
Fault
does not affect other affect the entire process
Tolerance
processes. and other threads in it.

Example: Creating a Thread by Extending the Thread Class

// 1. Define a class that extends the Thread class

class MyThread extends Thread {

// 2. Override the run() method to define the code the thread will
execute

@Override
public void run() {

// This is the code that will run in the new thread

System.out.println("Thread is running: " +


Thread.currentThread().getName());

public class ThreadExample {

public static void main(String[] args) {

// 3. Create an instance of MyThread

MyThread thread1 = new MyThread();

// 4. Start the thread

thread1.start(); // The run() method is invoked when start() is called

// You can create and start another thread as well

MyThread thread2 = new MyThread();

thread2.start(); // This also calls run() in a separate thread

OUTPUT :-

Thread is running: Thread-0

Thread is running: Thread-1

import java.sql.*;

import java.util.Scanner;
public class StudentDatabase {

// Database connection details

static final String DB_URL = "jdbc:mysql://localhost:3306/studentdb";

static final String USER = "root"; // replace with your MySQL username

static final String PASS = "password"; // replace with your MySQL password

// Method to perform update or delete operation

public static void executeUpdateDelete(String query, int id, String name, int age, String
grade) {

try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);

PreparedStatement stmt = conn.prepareStatement(query)) {

if (query.startsWith("UPDATE")) {

stmt.setString(1, name);

stmt.setInt(2, age);

stmt.setString(3, grade);

stmt.setInt(4, id);

} else if (query.startsWith("DELETE")) {

stmt.setInt(1, id);

int rowsAffected = stmt.executeUpdate();

System.out.println(rowsAffected > 0 ? "Operation successful." : "No record found with


ID: " + id);

} catch (SQLException se) {

se.printStackTrace();
}

public static void main(String[] args) {

try (Scanner scanner = new Scanner(System.in)) {

while (true) {

System.out.println("Choose operation:\n1. Update\n2. Delete\n3. Exit");

int choice = scanner.nextInt();

if (choice == 3) break;

System.out.print("Enter student ID: ");

int id = scanner.nextInt();

scanner.nextLine(); // consume newline

if (choice == 1) {

System.out.print("Enter new name: ");

String name = scanner.nextLine();

System.out.print("Enter new age: ");

int age = scanner.nextInt();

scanner.nextLine(); // consume newline

System.out.print("Enter new grade: ");

String grade = scanner.nextLine();

executeUpdateDelete("UPDATE students SET name = ?, age = ?, grade = ? WHERE


id = ?", id, name, age, grade);

} else if (choice == 2) {

executeUpdateDelete("DELETE FROM students WHERE id = ?", id, null, 0, null);

} else {
System.out.println("Invalid option.");

OUPUT:-

Choose operation:

1. Update

2. Delete

3. Exit

Enter student ID: 1

Enter new name: John Doe

Enter new age: 22

Enter new grade: A

Operation successful.

Choose operation:

1. Update

2. Delete

3. Exit

Enter student ID: 2

Operation successful.

You might also like