B.TECH COMPUTER SCIENCE AND ENGINEERING [AI-ML]
YEAR : 2nd
SEMESTER : 4th
SUBJECT : FORMAL LANGUAGE AND AUTOMATA THEORY
SUBJECT CODE : PCC-CSM404
Presentation Topic: Inheritance of interface in java
SUBMITED TO :
KAUSTAV ROY
BRAINWARE UNIVERSITY
Department Of Computer Science And Engineering - Presented by SOHAM MATHUR
BWU/BTA/21/130
What is an 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.
• It is used to achieve abstraction and multiple inheritance in java.
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.
How to Declare an interface
• An interface is declared by using the interface keyword.
• 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.
Syntax
interface<interface_name>
{
//declare constant fields
//declare methods that abstract
//by default
}
The relationship between 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.
Class interface interface
extends implements extends
Class Class interface
Java Interface Example
{
void draw();
}
//Implementation: by second user class
Rectangle implements drawable
{
public void draw()
{
System.out.println(“drawing rectangle”);
}
}
Class Circle implements Drawable
{
public void draw()
{
System.out.println(“drawing circle”);
}
}
//using interface: by third user public class interface
{
public static void main(String args[])
{
Drawable d=new Circle(); //In real scenario, object
is provided by method e.g.
get Drawable()
Drawable r=new Rectangle();
d.draw();
r.draw();
}
}
//Interface declaration: by first user interface Drawable
Multiple inheritance in Java by interface
• If a class implements multiple interfaces, or an interface extends multiple
interfaces, it is known as multiple inheritance.
Interface Interface interface interface
implements extends
class interface
Multiple inheritance using interface
Interface printable
{
void print();
}
Interface showable
{
Void show();
}
Public class A7 implements printable, showable
{
public void print()
{
System.out.println(“Hello”)
}
public void show()
{
System.out.println(“Welcome”);
}
public static void main(String args[])
{
A7 obj = new A7();
obj.print();
obj.show();
}
}
JAVA.pptx

JAVA.pptx

  • 1.
    B.TECH COMPUTER SCIENCEAND ENGINEERING [AI-ML] YEAR : 2nd SEMESTER : 4th SUBJECT : FORMAL LANGUAGE AND AUTOMATA THEORY SUBJECT CODE : PCC-CSM404 Presentation Topic: Inheritance of interface in java SUBMITED TO : KAUSTAV ROY BRAINWARE UNIVERSITY Department Of Computer Science And Engineering - Presented by SOHAM MATHUR BWU/BTA/21/130
  • 2.
    What is aninterface • 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. • It is used to achieve abstraction and multiple inheritance in java.
  • 3.
    Why use Javainterface • It is used to achieve abstraction. • By interface, we can support the functionality of multiple inheritance. • It can be used to achieve loose coupling.
  • 4.
    How to Declarean interface • An interface is declared by using the interface keyword. • 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.
  • 5.
  • 6.
    The relationship betweenclasses 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. Class interface interface extends implements extends Class Class interface
  • 7.
    Java Interface Example { voiddraw(); } //Implementation: by second user class Rectangle implements drawable { public void draw() { System.out.println(“drawing rectangle”); } } Class Circle implements Drawable { public void draw() { System.out.println(“drawing circle”); } } //using interface: by third user public class interface { public static void main(String args[]) { Drawable d=new Circle(); //In real scenario, object is provided by method e.g. get Drawable() Drawable r=new Rectangle(); d.draw(); r.draw(); } } //Interface declaration: by first user interface Drawable
  • 8.
    Multiple inheritance inJava by interface • If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. Interface Interface interface interface implements extends class interface
  • 9.
    Multiple inheritance usinginterface Interface printable { void print(); } Interface showable { Void show(); } Public class A7 implements printable, showable { public void print() { System.out.println(“Hello”) } public void show() { System.out.println(“Welcome”); } public static void main(String args[]) { A7 obj = new A7(); obj.print(); obj.show(); } }