Interfaces in Java Week 8 Session 1
Interfaces in Java Week 8 Session 1
JAVA
Outline
2
Recap
What is an interface
Examples
Difference between abstract class and interface
References
Recap
3
Bird
Interface
<<IWritable>>
Example
draw()
• There can be only abstract methods in the Java interface, not method body.
• In other words, you can say that interfaces can have abstract methods and
variables.
It 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 (Syntax)
interface abc
{
// declare constant fields
// declare methods that abstract
// by default.
}
Interface fields are public, static and final by default, and the
methods are public and abstract.
Relationship b/w classes and interfaces
As shown in the figure given below, a class extends another class, an interface
extends another interface, but a class implements an interface.
Interface (Example)
//Interface declaration: by first user
interface Drawable
{
void draw();
} //Main method
//Implementation: by second user public static void main(String args[])
class Rectangle implements Drawable {
{ Drawable d=new Circle;
public void draw() d.draw();
{System.out.println("drawing rectangle");} }
}
class Circle implements Drawable
{
public void draw()
{System.out.println("drawing circle");}
}
Interface (Example explanation)
The implementation part is hidden by the user who uses the interface.
When to use Interface?
• In such case, the end user may not be forced to override all the
methods of the interface.
Example
interface A
{
void a();
void b();
void c(); //Main method
void d(); public static void main(String args[])
} {
abstract class B implements A A a=new M();
{ a.a();
public void c() {System.out.println("I am c");} a.b();
} a.c();
a.d();
class M extends B }
{
public void a() {System.out.println("I am a");}
public void b() {System.out.println("I am b");}
public void d() {System.out.println("I am d");}
}
Conclusion of abstract class and interface
(important point)
• Abstract class is a concept and implementation gets completed when it is being realized
by a subclass.
• Abstract classes provide elements of both inheritance and interfaces (see example on
previous slide).
• Some or all members of the class might be unimplemented, and it is up to the inheriting
class to provide that implementation.
• Members that are implemented might still be overridden, and the inheriting class can still
implement additional interfaces or other functionality.
Difference between abstract class and interface
Feature Interface Abstract class
Multiple Inheritance A class may implement several A class may inherit only one
interfaces. abstract class.
Default An interface is purely abstract, it cannot An abstract class can provide
implementation provide any code, just the signature. complete, default code and/or
just the details that have to be
overridden.
Access modifiers An interface cannot have access modifiers An abstract class can contain
for the method, properties etc. Everything access modifiers for the
is assumed as public. methods, properties etc.
Core vs. Peripheral Interfaces are used to define the An abstract class defines the
peripheral abilities of a class. In other core identity of a class and there
words both Human and Vehicle can inherit it is used for related objects.
from a IMovable interface.
Homogeneity If various implementations only share If various implementations are of
method signatures then it is better to use the same kind and use common
Interfaces. behavior or status then abstract
class is better to use.
Difference between abstract class and interface
Feature Interface Abstract class
Fields and Constants No fields can be defined in interfaces An abstract class can have
fields and constants defined
Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java
abstract class can have instance methods that implements a default behavior. ... An abstract class may contain
non-final variables. Members of a Java interface are public by default.
An interface in Java is a specification of method prototypes. ... The user who want to use the methods of
the interface, he only knows the classes that implement this interface and their methods, information
about the implementation is completely hidden from the user, thus achieving 100% abstraction.
“is-a” and "can-do" analogy
• https://www.javatpoint.com/abstract-class-in-java
• https://www.geeksforgeeks.org/abstract-classes-in-java/?ref=lbp
21
THANK YOU