[go: up one dir, main page]

0% found this document useful (0 votes)
21 views25 pages

Interface

An interface in Java is a blueprint for classes, containing only abstract methods and static constants, defined using the 'interface' keyword. It allows for total abstraction, where all methods must be implemented by any class that uses the interface, and supports multiple inheritance. Key characteristics include that interfaces cannot be instantiated, do not contain constructors, and can extend multiple interfaces.

Uploaded by

vkmuneer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views25 pages

Interface

An interface in Java is a blueprint for classes, containing only abstract methods and static constants, defined using the 'interface' keyword. It allows for total abstraction, where all methods must be implemented by any class that uses the interface, and supports multiple inheritance. Key characteristics include that interfaces cannot be instantiated, do not contain constructors, and can extend multiple interfaces.

Uploaded by

vkmuneer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Interface in Java

Muneer Hussain
Assistant Professor in Computer Science
Sullamussalam Science College, Areekode
• An interface in Java is a blueprint of a class.

• It has static constants and abstract methods.

• There can be only abstract methods in the Java


interface, not method body.

• An interface is defined with an interface keyword.


Don’t Forget
• An interface is declared by using the
interface keyword.
• It provides total abstraction; 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.
Why Interface?
Syntax
interface <interface_name>{

// declare constant fields


// declare abstract methods
// by default.
}
Representation
Relationship between class and
interface
Simplest example
Class interface association
Multiple inheritance
Try this
Why??

Multiple inheritance is not


supported through class in java,
but it is possible by an
interface, why?
Code it
• Some hints about interface
➢ Interface method should be public and abstract.
➢ Interface fields should be public and final.
➢ Use the Keyword interface to define an interface.
➢ If you define a public interface with name myInterface the java
file should be named as myInterface.java (Similar to public
class definition rules).
➢ A class implementing an interface should use the
keyword implements.
➢ No objects can be created from an interface.
➢ Interfaces don't have constructors as they can't be
initiated
➢ An Interface can extends one or more interfaces.
➢ You can define a reference of type interface but
you should assign to it an object instance of class
type which implements that interface.
• A class implements an interface if it provides a
complete set of methods defined by this interface.
1) any number of classes may implement an interface
2) one class may implement any number of interfaces
• Each class is free to determine the details of its
implementation.
• Implementation relation is written with the
implements keyword.
• an interface is different from a class in several ways,
including:
• 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.
• An interface can extend multiple interfaces.
Try this

// Interface
interface Animal
{
public void animalSpecies();
public void animalSound();
public void animalHorns();
}
// Cow class "implements" the Animal interface
class Cow implements Animal
{
public void animalSpecies()
{
// The body of animalSound() is provided here
System.out.println("The Species of Cow is: Angus Cattle");
}
public void animalSound()
{
// The body of animalSound() is provided here
System.out.println("The Cow says: moo moo");
}
public void animalHorns()
{
// The body of animalHorns() is provided here
System.out.println("Cow has 2 horns");
}
}
public class InterfaceDemo
{
public static void main(String[] args)
{
Cow myCow = new Cow(); // Create a
Cow object
//calling methods of class cow
myCow.animalSpecies();
myCow.animalSound();
myCow.animalHorns();
}
}
• Consider interfaces A and B.
• interface {
void meth1();
void meth1(); void meth2();

void meth2(); extends


• }
void meth1();
• B extends A: void meth2();
• interface extends { void meth3();
void meth3();
• }
• MyClass must implement all of A and B methods: void meth1();
class MyClass implements { void meth2();
public void meth1() { void meth3();
System.out.println("Implement meth1().");
} implements
public void meth2() { MyClass
System.out.println("Implement meth2()."); public void meth1()
} {..}
public void meth2()
public void meth3() {
{…}
System.out.println("Implement meth3()."); public void meth3()
} {…}

}
Interface Abstract
All methods in an interface are An abstract class may contain
implicitly abstract. both abstract and non-abstract
methods.
A class may implement a A class can extend only one
number of Interfaces. abstract class.
In order for a class to implement A class may not implement all
an interface, it must implement declared methods of an abstract
all its declared methods. class.
Variables declared in a Java An abstract class may contain
interface is by default final. non-final variables.
Members of a Java interface are A member of an abstract class
public by default. can either be private, protected
or public.
class and interface

You might also like