0% found this document useful (0 votes)
7 views

Two Interfaces Program Explanation

Uploaded by

snikith7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Two Interfaces Program Explanation

Uploaded by

snikith7
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

// Define the first interface for addition

interface Addition {
int add(int a, int b);
}

// Define the second interface for subtraction


interface Subtraction {
int subtract(int a, int b);
}

// Define a class that implements both Addition and Subtraction interfaces


class Calculator implements Addition, Subtraction {
@Override
public int add(int a, int b) {
return a + b;
}

@Override
public int subtract(int a, int b) {
return a - b;
}
}

// Define the Main class


public class Main {
public static void main(String[] args) {
Calculator ccl = new Calculator();

// Perform addition
int resultAddition = ccl.add(5, 3);
System.out.println("Addition result: " + resultAddition);

// Perform subtraction
int resultSubtraction = ccl.subtract(5, 3);
System.out.println("Subtraction result: " + resultSubtraction);
}
}

-----------------------
Explanation
1. Interfaces Definition:
o Addition Interface: This interface declares a method add that takes two integers and returns their
sum.
interface Addition {
int add(int a, int b);
}
o Subtraction Interface: This interface declares a method subtract that takes two integers and
returns their difference.
interface Subtraction {
int subtract(int a, int b);
}
2. Calculator Class:
o This class implements both Addition and Subtraction interfaces, meaning it provides concrete
implementations for the add and subtract methods.
class Calculator implements Addition, Subtraction {
@Override
public int add(int a, int b) {
return a + b;
}

@Override
public int subtract(int a, int b) {
return a - b;
}
}
o The @Override annotation indicates that these methods are implementations of the methods
declared in the interfaces.
3. Main Class:
o This is the entry point of the program. The main method is where the program starts execution.
public class Main {
public static void main(String[] args) {
Calculator ccl = new Calculator();

// Perform addition
int resultAddition = ccl.add(5, 3);
System.out.println("Addition result: " + resultAddition);

// Perform subtraction
int resultSubtraction = ccl.subtract(5, 3);
System.out.println("Subtraction result: " + resultSubtraction);
}
}
o Creating an Instance: An instance of Calculator is created.
o Performing Operations: The add and subtract methods are called on the Calculator instance with
the arguments 5 and 3.
o Output: The results of the addition and subtraction are printed to the console.
In summary, this program demonstrates how to use interfaces to define methods for addition and
subtraction, and how to implement these methods in a class. The Main class then creates an
instance of the Calculator class and uses it to perform and display the results of these operations.

Java Program: Creating Two Threads with the Runnable Interface


This program creates two threads using the Runnable interface. Each thread prints numbers from
1 to 5 with a 500-millisecond delay between each number.
class NumberPrinter implements Runnable {
private String threadName;

// Constructor to initialize thread name


public NumberPrinter(String threadName) {
this.threadName = threadName;
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " prints: " + i);
Thread.sleep(500); // 500 ms delay
}
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
}

public class Main {


public static void main(String[] args) {
// Create two instances of NumberPrinter with different names
Runnable task1 = new NumberPrinter("Thread 1");
Runnable task2 = new NumberPrinter("Thread 2");

// Create Thread objects for each task


Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);

// Start both threads


thread1.start();
thread2.start();
}
}
Explanation of the Code:
1.NumberPrinter Class: This class implements Runnable and overrides the run() method. It
contains a loop that prints numbers from 1 to 5 with a 500-millisecond delay.
2.Main Class: In the main method:
o Two instances of NumberPrinter are created, each initialized with a unique name.
o Two Thread objects are then created using these Runnable instances.
o The start() method is called on each thread, beginning their execution.
Each thread runs independently, printing numbers with a slight delay, demonstrating basic
multithreading with the Runnable interface.

You might also like