[go: up one dir, main page]

0% found this document useful (0 votes)
5 views24 pages

Chapter 7 Interfaces

The document provides an overview of interfaces in Java, explaining their role in achieving abstraction and multiple inheritance. It outlines the differences between classes and interfaces, the syntax for defining and implementing interfaces, and the benefits of using interfaces for flexible and maintainable code. Key points include that interfaces cannot be instantiated, contain only abstract methods, and can be extended by other interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views24 pages

Chapter 7 Interfaces

The document provides an overview of interfaces in Java, explaining their role in achieving abstraction and multiple inheritance. It outlines the differences between classes and interfaces, the syntax for defining and implementing interfaces, and the benefits of using interfaces for flexible and maintainable code. Key points include that interfaces cannot be instantiated, contain only abstract methods, and can be extended by other interfaces.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Java Programming Language

Interfaces
Interfaces Introduction

• As you've already learned, objects define their


interaction with the outside world through the
methods that they exposed to.
• Interface looks like class but it is not a class.
• An interface can have methods and variables just like
the class but the methods declared in interface are by
default abstract (only method signature, no body).
• The interface in java is a mechanism to achieve
abstraction. It is used to achieve abstraction and
multiple inheritance in Java.
What is an Interface?

• An interface is basically a kind of class—it contains


methods and variables, but they have to be only
abstract methods and final fields/variables.
• Writing an interface is similar to writing a class. But a
class describes the attributes and behaviors of an
object. And an interface contains behaviors that a
class implements.
• Interface is a conceptual entity similar to a Abstract
class.
Class Interface
The keyword used to create a class is The keyword used to create an interface is
“class” “interface”
A class can be instantiated i.e, objects of a An Interface cannot be instantiated i.e, objects
class can be created. cannot be created.
Classes does not support multiple
Interface supports multiple inheritance.
inheritance.
It can inherit another class. It cannot inherit a class.
It can be inherited by a class by using the
It can be inherited by another class using
keyword ‘implements’ and it can be inherited
the keyword ‘extends’.
by an interface using the keyword ‘extends’.
It can contain constructors. It cannot contain constructors.
It cannot may/may not abstract methods. It contains abstract methods only.
Variables and methods in a class can be
All variables and methods in a interface are
declared using any access specifier(public,
declared as public.
private, default, protected)
Variables in a class can be static, final or
All variables are static and final.
neither.
Why the use of interfaces?

• Use when a number of classes share a common


interface.
• Interface in java is core part of Java programming
language and one of the way to achieve abstraction in
Java along with abstract class.
• Since multiple inheritance is not allowed in Java
through classes, interface is only way to implement
multiple inheritance.
Contd.
• It is the responsibility of the class that implements an interface to
supply the code for methods. Also, the variables declared in an
interface are public, static & final by default.
In other words, Interface fields are public, static and final
by default, and methods are public and abstract.
Interface - Example

<<Interface>>
Speaker
speak()

Politician Priest Lecturer


speak() speak() speak()
Interfaces Definition
• Syntax (appears like abstract class):

interface InterfaceName

{
• Example: // Constant/Final Variable Declaration
// Methods Declaration – no method
body
}
interface Speaker
{
public void speak( );
}
Implementing Interfaces
• Interfaces are used like super-classes whos properties are
inherited by classes. This is achieved by creating a class that
implements the given interface as follows:

class ClassName implements InterfaceName [, InterfaceName2, …]


{
// Body of Class
}
Implementing Interfaces Example

class Politician implements Speaker


{
public void speak(){
System.out.println(“Talk politics”);
}
}
class Priest implements Speaker
{
public void speak(){
System.out.println(“Religious Talks”);
}
}
class Lecturer implements Speaker
{
public void speak(){
System.out.println(“Talks Object Oriented Design and
Programming!”);
}
}
Diamond problem of multiple inheritance.
Contd.

• A class can implement any number of interfaces, but


cannot extend more than one class at a time.
• Therefore, interfaces are considered as an informal way of
realizing multiple inheritance in Java.
Extending Interfaces

• Like classes, interfaces can also be extended. The new sub-


interface will inherit all the members of the super interface in
the manner similar to classes.

• This is achieved by using the keyword extends as follows:

interface InterfaceName2 extends InterfaceName1


{
// Body of InterfaceName2
}
Interfaces Co relation
Forms of Interfaces
Inheritance and Interface Implementation
• A general form of interface implementation:

class ClassName extends SuperClass implements InterfaceName [,


InterfaceName2, …]
{
// Body of Class
}

• This shows a class can extended another class while implementing


one or more interfaces. It appears like a multiple inheritance (if we
consider interfaces as special kind of classes with certain restrictions
or special features).
Student Assessment Example
• Consider a university where students who
participate in the national games or Olympics
are given some grace marks.
• Therefore, the final marks awarded =
Exam_Marks + Sports_Grace_Marks.
• A class diagram representing this scenario is as
follow:
Student Sports

extends

Exam
implements
extends

Results
Software Implementation
class Student
{
// student info. and access methods
}
interface Sports
{
// sports grace marks (say 5 marks) and abstract methods
public static final int Sports_Grace_Marks = 5;
void calPerformance();
}
class Exam extends Student
{
// example marks (test1 and test 2 marks) and access methods
}
class Results extends Exam implements Sports
{
// implementation of abstract methods of Sport interface
// other methods to compute total marks = test1+test2+sports_grace_marks;
// other display or final results access methods
}
Interfaces and Software Engineering
• Interfaces, like abstract classes and methods, provide templates
of behaviour that other classes are expected to implement.

• Pass method descriptions, not implementation

• Java allows for inheritance from only a single superclass.


Interfaces allow for class mixing. Classes implement multiple
interfaces.
Key Points about Interfaces

• We can’t instantiate an interface in java.


• Interface provides complete abstraction as none of its methods
can have body. On the other hand, abstract class provides partial
abstraction as it can have abstract and concrete(methods with
body) methods both.
• “implements” keyword is used by classes to implement an
interface.
• While providing implementation in class of any method of an
interface, it needs to be mentioned as public.
• Class implementing any interface must implement all the
methods, otherwise the class should be declared as “abstract”.
• Interface cannot be declared as private, protected or transient.
Contd.

• All the interface methods are by default abstract and public.

• Variables declared in interface are public, static and final by


default.

• Interface variables must be initialized at the time of declaration

• Inside any implementation class, you cannot change the


variables declared in interface because by default, they are
public, static and final.

• Any interface can extend any interface but cannot implement


it. Class implements interface and interface extends interface.
Contd.
• A class can implement any number of interfaces.
• If there are two or more same methods in two
interfaces and a class implements both interfaces,
implementation of the method once is enough.
• A class cannot implement two interfaces that have
methods with same name but different return type.
• Variable names conflicts can be resolved by interface
name
When to use Interfaces? Advantages

• Interface is best choice for Type declaration or defining


contract between multiple parties.

• This brings us lot of flexibility and speed in terms of coding


and development.

• Flexible and maintainable code

• Higher level abstraction


Summary

• The benefits interface offers is it allows multiple inheritance which is


not possible with concrete classes and abstract classes.

• With interfaces, implements keyword is to be used instead of


general extends keyword. Implements informs the compiler that what
ever classes are there after implements, they are interfaces.

• An interface gives template structure of methods from which new


classes can be developed easily.

• The same abstract methods of the interface can be overridden


with different functionalities by different subclasses.

You might also like