0% found this document useful (0 votes)
3 views7 pages

Java Unit-3 Left

The document provides an overview of interfaces in Java, explaining their definition, implementation, and the concept of multiple inheritance through interfaces. It covers nested interfaces, inheritance of interfaces, default and static methods in interfaces, and functional interfaces, including examples for each concept. Additionally, it discusses annotations used to enforce the single abstract method rule in functional interfaces.

Uploaded by

gokinadinesh385
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)
3 views7 pages

Java Unit-3 Left

The document provides an overview of interfaces in Java, explaining their definition, implementation, and the concept of multiple inheritance through interfaces. It covers nested interfaces, inheritance of interfaces, default and static methods in interfaces, and functional interfaces, including examples for each concept. Additionally, it discusses annotations used to enforce the single abstract method rule in functional interfaces.

Uploaded by

gokinadinesh385
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

void callmetoo()

{
[Link]("This is a concrete method.");
}
}
class B extends A
{
void callme()
{
[Link]("B's implementation of callme.");
}
} Output
class AbstractDemo B's implementation of callme.
{ This is a concrete method.
public static void main(String args[])
{
B b = new B();
[Link]();
[Link]();
}
}

INTERFACES AND INHERITANCE


28. Differentiate interfaces and inheritance

INTERFACES: INTRODUCTION, DECLARATION OF INTERFACE, IMPLEMENTATION OF


INTERFACE
29. Describe concept of Interfaces.
Definition: An interface is a named collection of method definitions without implementations. Using
interface, you can specify what a class must do, but not how it does it. By providing the interface keyword,
Java allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism. Interfaces
in Java allows programmers to implement multiple inheritance, which would otherwise be impossible in
Java.
Interfaces and Classes : Interfaces are syntactically similar to classes, but they lack instance variables,
and, as a general rule, their methods are declared without any body. Once interface is defined, any number

28
of classes can implement an interface. Also, one class can implement any number of interfaces. To
implement an interface, a class must provide the complete set of methods required by the interface.
However, each class is free to determine the details of its own implementation.
Support to dynamic method resolution
Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method
to be called from one class to another, both classes need to be present at compile time so the Java compiler
can check to ensure that the method signatures are compatible. This requirement by itself makes for a static
and nonextensible classing environment. Inevitably in a system like this, functionality gets pushed up
higher and higher in the class hierarchy so that the mechanisms will be available to more and more
subclasses. Interfaces are designed to avoid this problem. They disconnect the definition of a method or set
of methods from the inheritance hierarchy. Since interfaces are in a different hierarchy from classes, it is
possible for classes that are unrelated in terms of the class hierarchy to implement the same interface.
Defining an Interface
An interface is defined much like a class.
General form:
access interface name
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
//...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
When no access modifier is included, then default access results, and the interface is only available to other
members of the package in which it is declared. When it is declared as public, the interface can be used by
any other code. In this case, the interface must be the only public interface declared in the file, and the file
must have the same name as the interface. name is the name of the interface, and can be any valid identifier.
Notice that the methods that are declared have no bodies. They end with a semicolon after the parameter
list. They are, essentially, abstract methods. Each class that includes such an interface must implement all
of the methods.
Variables can be declared inside of interface declarations. They are implicitly final and static, meaning they
cannot be changed by the implementing class. They must also be initialized. All methods and variables are
implicitly public.
Example
interface Drawable
{
void draw();
}

30. Explain implementation of interfaces with sample program.


Once an interface has been defined, one or more classes can implement that interface. To implement an
interface, include the implements clause in a class definition, and then create the methods required by the
interface.
General form :
class classname [extends superclass] [implements interface [,interface...]]
{

29
// class-body
}
If a class implements more than one interface, the interfaces are separated with a comma. If a class
implements two interfaces that declare the same method, then the same method will be used by clients of
either interface. The methods that implement an interface must be declared public. Also, the type signature
of the implementing method must match exactly the type signature specified in the interface definition. It
is both permissible and common for classes that implement interfaces to define additional members of
their own.

Example
class Rectangle implements Drawable
{
public void draw()
{
[Link]("drawing rectangle");
}
}

Program Output
interface Drawable //Interface declaration: by first user
{ drawing circle
void draw ();
}
class Rectangle implements Drawable //Implementation: by second user
{
public void draw ()
{
[Link] ("drawing rectangle");
}
}
class Circle implements Drawable
{
public void draw ()
{
[Link] ("drawing circle");
}
}
class TestInterface1 //Using interface: by third user
{
public static void main (String args[])
{
Drawable d = new Circle (); //In real scenario, object is provided by method e.g. getDrawable()
[Link] ();
}

MULTIPLE INTERFACES
31. Explain Multiple interfaces
Multiple inheritance in java can be achieved through interfaces:

30
Program
interface Printable
{
void print();
}
interface Showable
{
void show();
} Output
class A7 implements Printable,Showable Hello
{ Welcome
public void print()
{
[Link]("Hello");
}
public void show()
{
[Link]("Welcome");
}
public static void main(String args[])
{
A7 obj = new A7();
[Link]();
[Link]();
}
}

NESTED INTERFACES
32. What are nested interfaces?
An interface i.e. declared within another interface or class is known as nested interface. The nested
interfaces are used to group related interfaces so that they can be easy to maintain. The nested interface
must be referred by the outer interface or class. It can't be accessed directly.
There are given some points that should be remembered by the java programmer.
• Nested interface must be public if it is declared inside the interface but it can have any access modifier
if declared within the class.
• Nested interfaces are declared static implicitly.
Program
interface Showable
{ Output
void show(); Hello nested interface
interface Message
{
void msg();
}
}
class TestNestedInterface1 implements [Link]
{
public void msg()
{
[Link]("Hello nested interface");
}

31
public static void main(String args[])
{
[Link] message=new TestNestedInterface1();//upcasting here
[Link]();
}
}
As you can see in the above example, we are acessing the Message interface by its outer interface Showable
because it cannot be accessed directly. It is just like almirah inside the room, we cannot access the almirah
directly because we must enter the room first.

INHERITANCE OF INTERFACES
33. Demonstrate inheritance of interfaces
One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting
classes. When a class implements an interface that inherits another interface, it must provide
implementations for all methods defined within the interface inheritance chain.
Program
interface A
{
void meth1();
void meth2();
}
interface B extends A // B now includes meth1() and meth2() -- it adds meth3().
{
void meth3();
}
class MyClass implements B // This class must implement all of A and B
{
public void meth1()
{
[Link]("Implement meth1().");
}
public void meth2()
{
[Link]("Implement meth2().");
}
public void meth3()
{
[Link]("Implement meth3().");
}
} Output
class IFExtend Implement meth1().
{ Implement meth2().
public static void main(String arg[]) Implement meth3().
{
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}

32
DEFAULT METHODS IN INTERFACES
34. Explain default methods in interfaces
Before Java 8, interfaces could have only abstract methods. The implementation of these methods has to
be provided in a separate class. So, if a new method is to be added in an interface, then its implementation
code has to be provided in the class implementing the same interface. To overcome this issue, Java 8 has
introduced the concept of default methods which allow the interfaces to have methods with implementation
without affecting the classes that implement the interface. Default methods are also known as defender
methods or virtual extension methods.
Program
interface TestInterface
{
public void square(int a); // abstract method
default void show() // default method
{
[Link]("Default Method Executed");
}
}
class TestClass implements TestInterface
{
public void square(int a) // implementation of square abstract method
{
[Link](a*a); Output
} 16
public static void main(String args[]) Default Method Executed
{
TestClass d = new TestClass();
[Link](4);
[Link](); // default method executed
}
}
STATIC METHODS IN INTERFACE
35. Explain static methods in interface
The interfaces can have static methods as well which is similar to static method of classes.

Program
interface TestInterface
{ // abstract method
public void square (int a);
static void show() // static method
{
[Link]("Static Method Executed");
}
}
class TestClass implements TestInterface
{
public void square (int a) // Implementation of square abstract method
{
[Link](a*a);
Output
}
16
public static void main(String args[])
Static Method Executed
{
33
TestClass d = new TestClass();
[Link](4);
[Link](); // Static method executed
}
}

FUNCTIONAL INTERFACES
36. Define functional interfaces
A functional interface is an interface that contains only one abstract method. They can have only one
functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of
a functional interface. A functional interface can have any number of default methods. Runnable,
ActionListener, Comparable are some of the examples of functional interfaces.

ANNOTATIONS
37. Explain Annotations
Annotation is used to ensure that the functional interface can’t have more than one abstract method. In
case more than one abstract methods are present, the compiler flags an ‘Unexpected @FunctionalInterface
annotation’ message.
Program
@FunctionalInterface Output
interface Square 25
{
int calculate(int x);
}
class Test
{
public static void main(String args[])
{
int a = 5;
// lambda expression to define the calculate method
Square s = (int x)->x*x;
// parameter passed and return type must be same as defined in the prototype
int ans = [Link](a);
[Link](ans);
}
}

34

You might also like