PRACTICAL5
PRACTICAL5
Examine callbacks
5.1.callbacks in C .
5.1Call backs in C:
In C programming, a callback function is a function that you pass as an
argument to another function. The called function can then invoke the
callback function at some point during its execution. Callbacks are
commonly used in scenarios where you want to provide custom behaviour
or processing without having to modify the called function.
#include<stdio.h>
#include<conio.h>
printf(“%d”,a+b);
printf(“%d”,a-b);
(fptr)(5,2);
void main()
getch();
}
5.2.interface and inner classes in Java.
In Java, interfaces and inner classes are both important concepts that
contribute to the language's flexibility and organization. Here’s an
overview of each, along with how they interact:
### Interfaces
**Definition:**
**Key Points:**
**Example:**
```java
interface Animal {
void eat();
void sleep();
}
interface Pet {
void play();
System.out.println("Dog is eating.");
System.out.println("Dog is sleeping.");
System.out.println("Dog is playing.");
```
**Definition:**
Inner classes are classes defined within the scope of another class. Java
supports several types of inner classes:
1. **Member Inner Classes:** These are defined inside another class but
outside of any method. They have access to the enclosing class’s
members.
2. **Static Nested Classes:** These are defined with the `static` keyword
and do not have access to instance members of the enclosing class. They
are similar to regular top-level classes but are nested within another class.
3. **Local Classes:** These are defined within a method or a block of
code. They can access local variables of the method if they are declared
final or effectively final.
**Examples:**
```java
class OuterClass {
class InnerClass {
void display() {
System.out.println(outerField);
```
```java
class OuterClass {
void display() {
System.out.println(staticField);
}
```
**Local Class:**
```java
class OuterClass {
void someMethod() {
class LocalClass {
void display() {
System.out.println(localVariable);
localClass.display();
```
**Anonymous Class:**
```java
interface Greeting {
void greet();
}
public class Main {
System.out.println("Hello, Anonymous!");
};
greeting.greet();
```
### Summary
- **Interfaces** are used to define a contract that classes must adhere to,
enabling polymorphism and multiple inheritance.
- **Inner classes** allow for logically grouping classes that are only used
in one place and can be used to access the members of the enclosing
class.