Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Interfaces in Java Week 8 Session 1

Uploaded by

adeebaa480
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Interfaces in Java Week 8 Session 1

Uploaded by

adeebaa480
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Interface in

JAVA
Outline
2

 Recap
 What is an interface
 Examples
 Difference between abstract class and interface
 References
Recap
3

 Abstract class and method override, constructor of abstract class


2. Interface

• These are all mammals and share some


common behavior and properties. Like they
are warm blooded, they can have fur or hair,
they nourish their young with milk etc.
Mamma
• Bat has a special behavior i.e. that it can fly. ls
Flying is the behavior which is only available
Example: in Bat and not in other mammal.
• Birds can also fly.
• The Bat and Bird belongs to two different
categories but they have a common flying Can
behavior between them. fly
• In this scenario we can create an IFlyable
interface and make the Bat and Bird class
implement this interface.

Bird
Interface
<<IWritable>>
Example
draw()

• Fountain pen and ball point pen


are used for writings, they are
related entities. What should be Pen
the abstract class for these color :
String
entities? draw()

• We can write using pen but we


can also write with charcoal. Charcoal Ball point pen Fountain pen
Over here what is the common burn() draw() draw()
draw() refill()
behavior between pen and
charcoal that can be abstracted?
Person
write(IWritable :
instrument)
Interface
• An interface in Java is a blueprint of a class. It has static constants and abstract
methods.

• The interface in Java is a mechanism to achieve abstraction.

• There can be only abstract methods in the Java interface, not method body.

• It is used to achieve abstraction and multiple inheritance in java.

• In other words, you can say that interfaces can have abstract methods and
variables.

• It cannot have a method body.

• Java Interface also represents the IS-A relationship.


How to declare an interface?

An interface is declared by using the interface keyword. It provides total


abstraction.

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)

In this example, the Drawable interface has only one method.

Its implementation is provided by Rectangle and Circle classes.

In a real scenario, an interface is defined by someone else, but its implementation


is provided by different implementation providers.

Moreover, it is used by someone else.

The implementation part is hidden by the user who uses the interface.
When to use Interface?

• Use an interface when an immutable contract is really


intended.

• Interfaces are better suited in situations when your


applications require many possibly unrelated object types
to provide certain functionality.

• Interfaces are better in situations in which you do not


need to inherit implementation from a base class.

• Interfaces can be used for multiple inheritance.


Another real scenario of abstract class and interface

• The abstract class can also be used to provide some implementation


of the 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).

• An abstract class is a class that cannot be instantiated itself; it must be inherited.

• 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

Adding functionality If we add a new method to an If we add a new method to an


(Versioning) Interface then we have to track abstract class then we have
down all the implementations of the the option of providing default
interface and define implementation implementation and therefore
for the new method. all the existing code might
work properly.

Fields and Constants No fields can be defined in interfaces An abstract class can have
fields and constants defined

How does interface provide abstraction?


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.
Difference between abstract class and interface

What is the difference between abstraction and interface?

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.

How does interface provide abstraction?

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

• Abstract classes are classes that can be purely abstract.


• It serves as a base for other classes. When a class is derived from a base
class, that derived class has an "is-a" relationship with the base.
• For example an Employee is a person and a Triangle is a shape so these
cases could easily justify a base class and “is-a” relationship.
• Interfaces are purely abstract and guarantee member invariance. They
represent "can-do" or an invariant contract that specifies "can do and
will always do as expected to do“.
• Its appropriate to use an interface when it will be used by many,
unrelated types or in situations when multiple inheritance is
required.
References
20

• https://www.javatpoint.com/abstract-class-in-java
• https://www.geeksforgeeks.org/abstract-classes-in-java/?ref=lbp
21

THANK YOU

You might also like