KEMBAR78
CSC 211 Lesson 4 Part5 Interfaces | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
8 views19 pages

CSC 211 Lesson 4 Part5 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)
8 views19 pages

CSC 211 Lesson 4 Part5 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/ 19

INTRODUCTION TO

INTERFACES

BY SAMWEL TARUS
Introduction to Interfaces.
 Defined as an abstract type used to specify the behavior of a class. It is a blueprint
of a behavior.
 Java Interface also represents the IS-A relationship
 A mechanism to achieve abstraction.
 Interfaces primarily define methods that other classes must implement.
 By default, variables in an interface are public, static, and final.
 It is similar to class (contains static constants and abstract methods)
 It is a collection of abstract methods / behaviors that a class implements.
 A class implements an interface, thereby inheriting the abstract methods of the
interface.
 It is used to achieve abstraction and multiple inheritances in Java.
 It is also used to achieve loose coupling
How an interface is similar to a class
 An interface can contain any number of methods.
 An interface is written in a file with a .java extension, with the name of the interface
matching the name of the file.
 The byte code of an interface appears in a .class file.
 Interfaces appear in packages, and their corresponding bytecode file must be in a
directory structure that matches the package name.
How an interface is different from a class:
 You cannot instantiate an interface.
 An interface does not contain any constructors.
 All of the methods in an interface are abstract.
 An interface cannot contain instance fields. The only fields that can appear in an
interface must be declared both static and final.
 An interface is not extended by a class; it is implemented by a class.
Why use Java interface?
 It is used to achieve abstraction.
 By interface, we can support the functionality of multiple inheritance.
 It can be used to achieve loose coupling.
 In industry, architect-level people create interfaces, and then it is given to
developers for writing classes by implementing interfaces provided.
 Using interfaces is the best way to expose our project’s API to some other
projects. In other words, we can provide interface methods to the third-party
vendors for their implementation.
 For example, HDFC bank can expose methods or interfaces to various shopping
carts.
 Programmers use interface to customize features of software differently for
different objects.
When to use Interface and Abstract Class?

 Use an abstract class when a template needs to be defined for a group of


subclasses
 Use an interface when a role needs to be defined for other classes, regardless
of the inheritance tree of these classes
Important facts about an Interface
 A Java class can implement multiple Java Interfaces. It is necessary that the class
must implement all the methods declared in the interfaces.
 Class should override all the abstract methods declared in the interface
 The interface allows sending a message to an object without concerning which
classes it belongs.
 Class needs to provide functionality for the methods declared in the interface.
 All methods in an interface are implicitly public and abstract
 An interface cannot be instantiated
Pto…
 An interface reference can point to objects of its implementing classes
 An interface can extend from one or many interfaces. Class can extend only one
class but implement any number of interfaces
 An interface cannot implement another Interface. It has to extend another
interface if needed.
 An interface which is declared inside another interface is referred as nested
interface
 At the time of declaration, interface variable must be initialized. Otherwise, the
compiler will throw an error.
 The class cannot implement two interfaces in java that have methods with same
name but different return type.
Interface declaration
 Use the keyword interface.
 Syntax:
interface <interface_name>{

// Declare constant fields


// Declare methods that are abstract by default
}
 Example:
public interface NameOfInterface {
// Any number of final, static fields
// Any number of abstract method declarations
}
Program Examples.

 TestClassInterface.java, InterfaceExample1.java

 DogClass.Java, InterfacePet.java

 InterfaceDriver1.java, testInterface.java

 InterfaceDriver2.java, VehicleInterface.java

 MammalClass.java, AnimalInterface.java

 ClassInterfaceDemo.java, CarIntercae.java
Properties of interface
 An interface is implicitly abstract. You do not need to use the abstract keyword while
declaring an interface.
 Each method in an interface is also implicitly abstract, so the abstract keyword is not
needed.
 Methods in an interface are implicitly public.

 Example:

interface Animal {
public void eat();
public void travel();
Implementing Interfaces
 When a class implements an interface, you can think of the class as signing a
contract, agreeing to perform the specific behaviors of the interface. If a class does
not perform all the behaviors of the interface, the class must declare itself as
abstract.
 A class uses the implements keyword to implement an interface. The implements
keyword appears in the class declaration following the extends portion of the
declaration.
Rules to be followed when overriding methods defined in interfaces
 Checked exceptions should not be declared on implementation methods other than
the ones declared by the interface method or subclasses of those declared by the
interface method.
 The signature of the interface method and the same return type or subtype should
be maintained when overriding the methods.
 An implementation class itself can be abstract and if so interface methods need not
be implemented.
Rules for implementing interfaces:
 A class can implement more than one interface at a time.
 A class can extend only one class, but implement many interfaces.
 An interface can extend another interface, similarly to the way that a class can
extend another class.
Extending Interfaces:
 An interface can extend another interface, similarly to the way that a class can
extend another class.
 The extends keyword is used to extend an interface, and the child interface
inherits the methods of the parent interface.

 InterfaceExtend.Java, ExtendingAnInterface.java, DriverMain1.java


Difference between abstract class and interface
Abstract Interface
 Can have abstract and non-  Can have only abstract methods. Since
abstract methods. Java 8, it can have default and static
methods also.
 Doesn't support multiple inheritance.  Supports multiple inheritance.
 Can have final, non-final, static and  Has only static and final variables.
non-static variables.
 Can provide the implementation of  Can't provide the implementation of
interface. abstract cla
 abstract keyword is used to declare  The interface keyword is used to
abstract class. declare interface.
 An abstract class can extend another  An interface can extend another Java
Java class and implement multiple interface only.
Java interfaces.
 An abstract class can be extended  An interface can be implemented
using keyword "extends". using keyw
 A Java abstract class can have class  Members of a Java interface are public
members like private, protected, etc. by default "implements".
 Example:  Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
} }

Simply, abstract class achieves partial abstraction (0 to 100%) whereas interface achieves fully
abstraction (100%).
Multiple Inheritance in Java Using Interface
 Multiple Inheritance is an OOPs concept that cannot be implemented in Java
using classes. (MultipleInheritance.java)
 Multiple inheritances in Java can be implemented using Interfaces.
Real-World Example:
 Let’s consider the example of vehicles like bicycle, car, bike………, they have
common functionalities. So we make an interface and put all these common
functionalities. And lets Bicycle, Bike, car ….etc implement all these
functionalities in their own class in their own way.

 MachineSystem.Java

 BankSystem.java
Advantages of Interfaces

 Without bothering about the implementation part, we can achieve the security
of the implementation.
 In Java, multiple inheritances are not allowed, however, you can use an
interface to make use of it as you can implement more than one interface.

You might also like