You are on page 1of 30

MALLA REDDY UNIVERSITY

JAVA Programming unit-III


Interfaces
Interfaces Vs Abstract Classes
Defining an Interface
Implement Interfaces
Accessing Implementations Through Interface References
Extending Interface
Inner Class

Department of CSE
Difference between abstract class and interface
Abstract class Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default
and static methods also.

2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.

3) Abstract class variables. can have final, non-final, static and non- Interface has only static and final variables.
static
4) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.

5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

6) An abstract class can extend another Java class and implement An interface can extend another Java interface only.
multiple Java interfaces.
7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements".

8) A Java abstract class can have class members like private, protected, Members of a Java interface are public by default.
etc.
9)Example: public abstract clasShape{ Example:
public abstract void draw(); public interface Drawable{
} void draw();
}
Department of CSE
Interface
• Another way to achieve abstraction in Java, is with interfaces.
• An interface in Java is a blueprint of a class. It has static constants and
abstract methods.
• There can be only abstract methods in the Java interface, not method
body.
• Variables can be declared inside of interface declarations, they are
implicitly final and static.
• All methods and variables are implicitly public.

Department of CSE
Why use Java interface?

1) It is used to achieve abstraction.

2) By interface, we can support the functionality of multiple inheritance.

Department of CSE
How to declare an interface?
An interface is declared by using the interface keyword.
It provides total abstraction; means all the methods in an interface are declared
with the empty body, and all the fields are public, static and final by default.

A class that implements an interface must implement all the methods declared
in the interface.

interface <interface_name> interface Callback


{ {
// declare constant fields void method(int p);
// declare methods that abstract by }
default
}
Department of CSE
Implement Interfaces:
• 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 defined by the interface.
• The general form of a class that includes the implements clause looks like
this:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
• If a class implements more than one interface, the interfaces are separated
with a comma.
Department of CSE
interface Callback {
void method(int p);
}
class Client implements Callback {
// Implement Callback's interface
public void method(int p) {
System.out.println("callback called with " + p);
}
void nonInterfaceMeth() {
System.out.println("Classes that implement interfaces " + "may also define other members, too.");
}
}
public class Main {
public static void main(String[] args) {
Client c = new Client();
c.method(42);
c.nonInterfaceMeth();
}
}
Department of CSE
The relationship between classes and interfaces

A class extends another class, an interface extends another interface, but


a class implements an interface.

Department of CSE
Example-1 using interfaces in Java
interface Inter {
int a = 10, b = 20; // Interface variables are implicitly public, static, and final

void add(); // Abstract method declaration


}

class C1 implements Inter {


public void add() {
int sum = a + b;
System.out.println("Sum of numbers is: " + sum);
}
public static void main(String[] args) {
C1 obj = new C1();
obj.add();
}
}
output: Sum of numbers is: 30
Department of CSE
Example of implementing interfaces with different classes
interface Drawable{
void draw();
}
class Rectangle implements Drawable{
public void draw() { System.out.println("drawing rectangle"); }
}
class Circle implements Drawable{
public void draw() { System.out.println("drawing circle"); }
}
class TestInterface1 {
public static void main(String args[]) {
Drawable d=new Circle();
d.draw();
Drawable d1=new Rectangle(); output: drawing circle
d1.draw(); drawing rectangle
} } Department of CSE
Accessing Interface:
Java Accessing Implementations Through Interface References
• We can declare variables as object references that use an interface
rather than a class type.
• Any instance of any class that implements the declared interface can
be referred to by such a variable.
• When you call a method through one of these references, the correct
version will be called based on the actual instance of the interface
being referred to.
• This process is similar to using a superclass reference to access a
subclass object.

Department of CSE
• The following example calls the callback() method via an interface reference variable:
interface Callback {
void callback(int param); }
class Client implements Callback {
// Implement Callback's interface
public void callback(int p) {
System.out.println("callback called with " + p);
}
void aa() {
System.out.println("hi.");
} }
public class Main{
public static void main(String args[]) {
Callback c = new Client();
c.callback(42);
} }
Department of CSE
The output of above program is :
callback called with 42
• The variable c is declared to be of the interface
type Callback, yet it was assigned an instance of Client.
• Although c can be used to access the callback() method, it
cannot access any other members of the Client class.
• An interface reference variable has knowledge only of the
methods declared by its interface declaration.
• Thus, c could not be used to access aa() since it is defined
by Client but not Callback.

Department of CSE
Extending Interface-----Interface inheritance:
A class implements an interface, but one interface extends another interface.

interface Printable{
void print();
}
interface Showable extends Printable{
void show();
}
class TestInterface4 implements Showable{
public void print() { System.out.println("Hello"); }
public void show() { System.out.println("Welcome"); }
public static void main(String args[]){
TestInterface4 obj = new TestInterface4();
obj.print();
obj.show();
} Out put : Hello
} Welcome
Department of CSE
Multiple inheritance in Java by interface
If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.

Department of CSE
interface inter public void sub()
{ {
int a=10,b=20; int r=c-d;
public abstract void add(); System.out.println(" Difference of numbers
} is :" +r);
interface inter1 }
{ public static void main(String[] args)
int c=20,d=10; {
public abstract void sub(); c1 obj=new c1();
} obj.add();
class c1 implements inter,inter1 obj.sub();
{ }
public void add() } Output: Sum of numbers is : 30
{ Difference of numbers is: 10
int sum=a+b;
System.out.println(" Sum of numbers
is :" +sum);
} Department of CSE
Java Nested Interface
An interface, i.e., declared within another interface or class, is known as a nested
interface. The nested interfaces are used to group related interfaces so that they
can be easy to maintain.

Points to remember

•The 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

Department of CSE
Syntax of nested interface which is declared within the interface

interface interface_name{
...
interface nested_interface_name{

...
}
} Syntax of nested interface which is declared within the class

class class_name{
...
interface nested_interface_name{
...
}
}
Department of CSE
Example of nested interface which is declared within the interface
interface Showable
{ public static void main(String args[])
void show(); {
interface Message c1 obj1=new c1();
{ obj1.msg();
void msg(); obj1.show();
} }
} }
class c1 implements Showable.Message
{ Output: Hello nested interface
public void msg() Welcome to nested interface
{
System.out.println("Hello nested interface");

public void show()


{
System.out.println("Welcome to nested interface");
}

Department of CSE
Example of nested interface which is declared within the class
class A public static void main(String args[]){
{
interface Message inter obj1=new inter();
{ obj1.show();
void msg(); obj1.msg();
void show(); }
} }
}
Output: Welcome to nested interface
class inter implements A.Message Hello nested interface
{
public void msg()
{
System.out.println("Hello nested interface");

}
public void show()
{
System.out.println(" Welcome to nested interface");
}
Department of CSE
Java Default Methods
Java provides a facility to create default methods inside the interface. Methods
which are defined inside the interface and tagged with default are known as default
methods. These methods are non-abstract methods.

interface add class c1 implements add{


{ public void addition()
public void addition(); {
default void addition1() int i=1,j=4,k;
{ int a=1,b=2,c; k=i+j;
c=a+b; System.out.println(k); }
System.out.println(c); public static void main(String[] args)
} { add obj=new c1();
static void sub() obj.addition();
{ obj.addition1();
int l=20,m=10,n; add.sub();
n=l-m; }
System.out.println(n); } output:5
} 3
} 10
Department of CSE
Inner class:
• In Java, inner class refers to the class that is declared inside class or
interface which were mainly introduced to group classes that belong
together, which makes your code more readable and maintainable.
• Java inner class can be declared private, public, protected, or with default
access whereas an outer class can have only public or default access.
• Syntax:
public class OuterCLass {
//outer code
public class innerClass {
//Inner class code
}
}

Department of CSE
There are 4 types of inner classes in Java:
1. Nested Inner class
2. Method local inner classes
3. Anonymous inner classes
4. Static nested classes

Department of CSE
1. Nested inner class in java
• As the name suggests, this type of inner class involves the nesting of a class inside another class.
The inner class can access the private variables of the outer class.
class Outer { // Simple nested inner class
class Inner { // show() method of inner class
public void show()
{ // Print statement
System.out.println("In a nested class method");
}
}
}
// Main class
class Main {
// Main driver method
public static void main(String[] args)
{ // Note how inner class object is created inside main()
Outer.Inner in = new Outer().new Inner(); // explained in next slide
// Calling show() method over above object created
in.show();
} } Department of CSE
Outer.Inner in = new Outer().new Inner();
• This line of code creates a new instance of the inner class Inner which is defined within
the outer class Outer. Let's break it down:

• new Outer(): This creates a new instance of the outer class Outer. Assuming Outer is a
class, this syntax initializes a new object of that class.

• .new Inner(): After creating an instance of the outer class, you use the .new syntax to
create an instance of the inner class Inner. Since Inner is a non-static inner class, it needs
an instance of the outer class to exist. So, by calling new Outer().new Inner(), you are
creating an instance of Inner associated with the instance of Outer that you just created.

• Outer.Inner in = ...: Finally, you declare a variable named in of type Outer.Inner to hold
the reference to the newly created instance of the inner class. Outer.Inner specifies that
Inner is a nested class within Outer.

• In summary, Outer.Inner in = new Outer().new Inner(); creates a new instance of the


inner class Inner, which is nested inside the outer class Outer, and assigns it to a variable
named in.

Department of CSE
2. Method local inner classes
• In the case of method local inner classes, the outer class method contains the inner class.
class Outer {
void outerMethod() {
int x = 98;
System.out.println("inside outerMethod");
class Inner {
void innerMethod() {
System.out.println("x= "+x);
}
}
Inner y = new Inner();
y.innerMethod();
} }
class Main {
public static void main(String[] args) {
Outer x=new Outer();
x.outerMethod();
} } output: inside outerMethod x= 98
Department of CSE
3. Anonymous inner classes
Anonymous inner classes are declared without any name at all. They
are created in two ways.
• As a subclass of the specified type
• As an implementer of the specified interface

Department of CSE
//As a subclass of the specified type--In this method, the anonymous class is put inside a subclass of the outer class.
class OuterClass {
void print() {
System.out.println("I am in the print method of superclass");
}
}
class AnonymousClass {
// An anonymous class with OuterClass as base class
//start of the anonymous class.
static OuterClass out = new OuterClass() {
void print() {
super.print();
System.out.println("I am in Anonymous class");
}
};
public static void main(String[] args) {
out.print();
}
} Output: I am in the print method of the superclass I am in Anonymous class

Department of CSE
//The implementer of specified Interface--The anonymous class can extend a class or implement an interface
// at a time.

interface Anonym {
void print();
}
class AnonymousClass {
// An anonymous class with OuterClass as base class
//start of the anonymous class.
static Anonym an = new Anonym() {
public void print() {
System.out.println("I am an implementation of interface Anonym");
}
};
public static void main(String[] args) {
an.print();
}
}
Department of CSE
4. Static nested classes--If you declare the inner class to be static, then you can access the
class without having to create an object of the outer class.
However, the static class will not have access to members of the outer class. We can access
the elements of the outer class from the inner class.
class OuterClass {
public static class InnerClass {
public void print() {
System.out.println("I am printing from a static inner class!");
}
}
}
public class StaticInnerClass {
public static void main(String[] args) {
OuterClass.InnerClass in =new OuterClass.InnerClass();
in.print();
}
}
Department of CSE

You might also like