0% found this document useful (0 votes)
139 views13 pages

Java Interfaces and Examples

- Interfaces in Java allow for multiple inheritance and polymorphism. They define behaviors but not implementations, which are defined by classes that implement the interfaces. - Interfaces can contain only abstract methods and final variables. All methods in an interface are public and abstract by default. - Classes can implement multiple interfaces. Interface variables can hold instances of any implementing class. This allows for runtime polymorphism.

Uploaded by

Shubhankar Singh
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)
139 views13 pages

Java Interfaces and Examples

- Interfaces in Java allow for multiple inheritance and polymorphism. They define behaviors but not implementations, which are defined by classes that implement the interfaces. - Interfaces can contain only abstract methods and final variables. All methods in an interface are public and abstract by default. - Classes can implement multiple interfaces. Interface variables can hold instances of any implementing class. This allows for runtime polymorphism.

Uploaded by

Shubhankar Singh
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
You are on page 1/ 13

Topics

• Interfaces in Java

1 Object-Oriented Programming Using Java


Interfaces in Java

• Java does not support Multiple-Inheritance directly. Multiple inheritance can be


achieved in java by the use of Interfaces.
• We need interfaces when we want functionality to be included but does not want
to impose implementation.
• Implementation issue is left to the individual classes implementing the interfaces.
• Interfaces can have only abstract methods and final fields.
• Every method in an interface is by default public abstract
• Every variable in an interface is by default public final
• You can declare a variable to be of type interface. But you can not create an
object belonging to type interface.
• Interface variable can point to objects of any class implementing the interface.
• Another way of implementing Run Time Polymorphism.
2 Object-Oriented Programming Using Java
Class vs Interfaces
(Similarities)
• is compiled into byte code file
• can be either public, protected, private or package
accessibility
• can not be public unless defined in the file having same
name as interface name
• serve as a type for declaring variables and parameters

3 Object-Oriented Programming Using Java


Class vs Interfaces
(Differences)
• Declares only Method Headers and public constants
• Has no constructors [So, an object never belongs to an
interface].
• Can be implemented by a class. A class can implement
multiple interfaces.
• Can not extend a class.
• Can extend several other interfaces.

4 Object-Oriented Programming Using Java


Interface Syntax : General Form

• Syntax :
<scope> interface <interface-name> extends [ <interface1> ,… ,<interface-N>]
{

[public][final] <type> variable-name-1 = value;


.
.
[public][final] <type> variable-name-N = value;

[public][abstract] <return type> method-name-1(<parameter lis>);


.
.
[public][abstract] <return type> method-name-N(<parameter lis>);
}

5 Object-Oriented Programming Using Java


Interface Example 1

public interface A Name of source file must be A.java


{
double PI = 3.14156; public final PI = 3.1456;
void show(); public abstract void show();
void display(); public abstract void display();
} // End of Interface A
class X implements A
{
public void show() { } Implemented Methods of
Interfaces should have
public void display() { }
public scope
}// End of class X
6 Object-Oriented Programming Using Java
Interface Example 2
public interface A
{
double PI = 3.14156;
void show();
void display();
} // End of Interface A
abstract class X implements A
A class should either fully
{ implement an interface or it
public void show() { } should be declared as
} abstract
class Y extends X
{
public void display() { }
}// End of class X

7 Object-Oriented Programming Using Java


Interface Example 3
interface A
{ An interface can extend
void show-1(); multiple interfaces
void display-1();
class X implements C
} // End of Interface A {
interface B void show-1() {}
{ void display-1() {}
void show-2(); void show-2() {}
void display-2(); void display-2() {}
void show-3() {}
} // End of Interface B void display-3() {}
interface C extends A, B } // End of class X
{
void show-3(); If a class implements a sub-interface
void display-3(); then it also implements its super
} // End of Interface C interfaces
8 Object-Oriented Programming Using Java
Interface Example 4

interface Area <<Interface>>


{
Area
double PI = 3.1456;
double area();
double perimeter();
} // End of Interface Area
interface Volume extends Area Volume
{
<<Interface>>
double volume();
} // End of Interface Volume

9 Object-Oriented Programming Using Java


Interface Example 4 ….
class Circle implements Area
{
private double radius;
Circle(double radius)
{
this.radius = radius;
}
double getRadius() { return radius;}
public double area()
{
return PI * radius * radius;
}
public double perimeter()
{
return 2 * PI * radius;
}
}// End of class Circle
10 Object-Oriented Programming Using Java
Interface Example 4 ….
class BOX implements Volume public double volume()
{ {
private double length; return length * width * height ;
private double width; } // End of Method
private double height; public double perimeter()
BOX(double l, double b, double h) {
{ double p = length+width+height;
length = l;
return 4 * p;
width = b;
}// End of Method
height = h;
} // End of class BOX
}
double getLength() { return length ;}
double getWidth() { return width ;}
double getHeight() { return height ;}
public double area()
{
return 2 * (length * width + width * height + height * length);
} // End of Method
11 Object-Oriented Programming Using Java
Runtime Polymorphism
Through Interfaces
• Suppose ‘X’ is an interface and three concrete classes namely ‘A’,
‘B’, and ‘C’ implements ‘X’ interface

Any Interface Type Variable Can Point


to Any Instance of a Class That
Implements the Interface

X x1 = new A();
x1.show(); Invokes show()
of class A

x1 = new B();
Invokes show()
x1.show();
of class B

12 Object-Oriented Programming Using Java


Thank You

13 Object-Oriented Programming Using Java

You might also like