0% found this document useful (0 votes)
10 views

Interface

Uploaded by

arunanirmal2004
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)
10 views

Interface

Uploaded by

arunanirmal2004
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/ 26

Interface

Presented by
Ms.G.Sakthi Priya, AP/CSE
Interface
• An interface in java is a blueprint of a class.
• The interface in java is a mechanism to achieve abstraction and multiple
inheritance
• Interface is similar to a class which contains method’s signature only but
not implementation.
• It has a formal set of method and constant declarations that must be
defined by the class which implements it.
• Interface is declared by using interface keyword. It provides total
abstraction (100% abstraction)
• All the methods declared in the interface are public and abstract
methods by default.
• All the data members are public, static, final members.
• Constructors are not allowed in interface
• We can’t create instance(interface can’t be instantiated) of interface
but we can make reference of it that refers to the Object of its
implementing class
Key points to remember about
interfaces
• Interface provides full abstraction as none of its methods 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 that implements any interface must implement all the methods
of that interface, else the class should be declared abstract
• Interface cannot be declared as private, protected
Defining Interface
Implementing Interface
• Once an interface has been defined, one or more classes can
implement that interface.
• To implement an interface, include the implements clause in a class
definition, and then create the methods required by the interface
Simple Example using Interface
Output
interface Vehicle{
void running(); //by default public abstract Running
}
class car implements Vehicle{
public void running(){
System.out.println("Running Car");
} Accessing implementation
} through
class Demo{ interface reference
public static void main(String args[]){
car c = new car(); car c = new car(); Vehicle v;
v=new car();
c.running(); Vehicle v;
v=c; v.running();
}
v.running(); All the three ways are valid and
}
produces the same result
Interface with variable
interface Vehicle{
int a=10; //By default public static final
void run();
}
class car implements Vehicle{
public void run(){
System.out.println("Running");
//a=20; cannot assign a value to final variable a
System.out.println(a);
}
}
class Demo{
public static void main(String args[]){
Output
Vehicle v; Running
v=new car(); 10
v.run();
}
}
Exp 6
• Write a Java Program to create an abstract class named Shape that
contains two integers and an empty method named printArea().
Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape. Each one of the
classes contains only the method printArea( ) that prints the area of
the given shape.
• Solve the above problem using an interface.
Design an interface Appliance with methods turnOn() and turnOff().
Implement this interface in classes Fan, Light, and AirConditioner. Each
appliance should print a message indicating whether it's turned on or
off.
Relationship between classes and
interface
• Any class can extend only 1 class (that’s why multiple inheritance is not
possible)
• Any class can implement an infinite number of interface
• An interface can extend another interface or interfaces (more than one
interface) only.
Multiple inheritance

Class A Class B

extends
Class C

• Java doesn’t support multiple inheritance.


• A class can’t extend more than one superclass.
Multiple inheritance in Java by
interface
• If a class implements multiple interfaces, or an interface extends
multiple interfaces i.e. known as multiple inheritance.
class Demo{
interface Father{ public static void main(String args[]){
void playwithchild(); kid k = new kid();
Example 1: Multiple Inheritance

} k.playwithchild();
interface Mother{ k.teachchild();
void playwithchild(); }
void teachchild();
}
}
class kid implements Father,Mother{
public void playwithchild(){
System.out.println("Father and Mother playing with child");
}
public void teachchild(){
System.out.println("Mother is teaching the child");
}
}
Class poll

Output
Implement meth1().
Implement meth2().
Implement meth3().
// Implement the third interface in a class
class MyClass implements C {
// Define the first interface
public void methodA() {
interface A { System.out.println("Method A");
Example 2: Multiple Inheritance

void methodA(); }
}
public void methodB() {
// Define the second interface System.out.println("Method B");
interface B { }
void methodB();
} public void methodC() {
System.out.println("Method C");
interface C extends A, B { }
void methodC(); }
}
public class Demo {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.methodA();
obj.methodB();
obj.methodC();
}
}
Class poll
• Is the below representation is possible?

Class Exam Interface intFA


Interface Sports Interface intFB extends intFA
class Result extends Exam implements Sports Class Sample implements intFB
Practice question
• Define three interfaces:
• Monitor with a method void startMonitoring().
• Alert with a method void sendAlert(String message).
• Recorder with a method void recordVideo().
• Implement a class called HomeSecuritySystem that implements all
three interfaces.
• Write a Main class that instantiates a HomeSecuritySystem object.
Demonstrates monitoring, sending an alert, and recording video.
Nested Interface
• An interface can be declared a member of a class or another
interface.
• Such an interface is called a member interface or a nested interface.
• A nested interface can be declared as public, private, or protected
Differentiate Abstract class and
interface
Abstract Class Interface

An abstract class can be extended using keyword Java interface can be implemented using keyword
“extends”. “implements”.

All the methods in an interface are treated as abstract


It can contain both abstract methods and non‐
methods even though there is no abstract keyword in
abstract methods.
the methods.

Abstract class can have final, non-final, static and non-


Interface has only static and final variables.
static variables.

An abstract class can extend another class and


An interface can extend another interface only.
implement multiple interfaces.
Abstract Class Interface

Abstract class doesn't support multiple Inheritance Interface supports multiple inheritance.

The abstract keyword is used to declare abstract class The interface keyword is used to declare interface

abstract class Shape{ interface Shape{


public abstract void draw(); void draw();
} }
• Design a Java interface for ADT Stack. Implement this interface using
array
• Design a Java interface for ADT Queue. Implement this interface using
array

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy