0% found this document useful (0 votes)
13 views6 pages

PRACTICAL5

Uploaded by

Abhishek Goud
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)
13 views6 pages

PRACTICAL5

Uploaded by

Abhishek Goud
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/ 6

Pratical :5

Examine callbacks
5.1.callbacks in C .

5.2.interface and inner classes in Java.

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>

void add(int a,int b)

printf(“%d”,a+b);

void sub(int a,int b)

printf(“%d”,a-b);

void display(void (*fptr) (int,int))

(fptr)(5,2);

void main()

display(add); //call back

display(sub); //call back

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:**

An interface in Java is a reference type, similar to a class, that can contain


only constants, method signatures, default methods, static methods, and
nested types. Interfaces cannot contain instance fields or constructors.

**Key Points:**

1. **Abstract Methods:** Interfaces can contain abstract methods


(methods without a body). Classes that implement the interface must
provide implementations for these methods.

2. **Default Methods:** Since Java 8, interfaces can also include default


methods with a default implementation. This allows you to add new
methods to interfaces without breaking existing implementations.

3. **Static Methods:** Interfaces can have static methods that must be


called on the interface itself rather than on instances of classes that
implement the interface.

4. **Inheritance:** An interface can extend other interfaces, forming a


hierarchy. A class implementing an interface must implement all the
methods declared in that interface and all its superinterfaces.

**Example:**

```java

interface Animal {

void eat();

void sleep();

}
interface Pet {

void play();

class Dog implements Animal, Pet {

public void eat() {

System.out.println("Dog is eating.");

public void sleep() {

System.out.println("Dog is sleeping.");

public void play() {

System.out.println("Dog is playing.");

```

### Inner Classes

**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.

4. **Anonymous Classes:** These are unnamed classes defined at the


point of instantiation. They are useful for quick implementations of
interfaces or abstract classes.

**Examples:**

**Member Inner Class:**

```java

class OuterClass {

private String outerField = "Outer field";

class InnerClass {

void display() {

System.out.println(outerField);

```

**Static Nested Class:**

```java

class OuterClass {

private static String staticField = "Static field";

static class StaticNestedClass {

void display() {

System.out.println(staticField);
}

```

**Local Class:**

```java

class OuterClass {

void someMethod() {

final String localVariable = "Local variable";

class LocalClass {

void display() {

System.out.println(localVariable);

LocalClass localClass = new LocalClass();

localClass.display();

```

**Anonymous Class:**

```java

interface Greeting {

void greet();

}
public class Main {

public static void main(String[] args) {

Greeting greeting = new Greeting() {

public void greet() {

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.

These features together enhance the structure and design of Java


applications, promoting better organization and encapsulation.

You might also like