[go: up one dir, main page]

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

CGR Unit2 Interfaces

Uploaded by

23f2002747
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)
21 views12 pages

CGR Unit2 Interfaces

Uploaded by

23f2002747
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/ 12

Interface

• The interface in Java is a mechanism to achieve abstraction.

• There can be only abstract methods in the Java interface, not method
body.

• Java Interface also represents the IS-A relationship.

• It cannot be instantiated just like the abstract class.

• Since Java 8, we can have default and static methods in an interface.

• Since Java 9, we can have private methods in an interface. //share


common code between non-abstract methods.
cgr 1
Interface
• Since interfaces are in a different hierarchy from classes, it is possible for
classes that are unrelated in terms of the class hierarchy to implement the
same interface.

• This is where the real power of interfaces is realized.

• By providing the interface keyword, Java allows you to fully utilize the
“one interface, multiple methods” aspect of polymorphism.

• Variables can be declared inside interface declarations. They are


implicitly final and static, meaning they cannot be changed by the
implementing class. They must also be initialized.

• All methods and variables are implicitly


cgr
public. 2
Interface
Why use Java interface?
 To enable classes to have behavior that does not belong to them.
 To model multiple inheritance.
 To achieve complete abstraction.
 To have loose coupling.

Page link to illustrate : https://


medium.com/geekculture/why-do-we-need-interfaces-in-java-9a95ef57a156

cgr 3
Interface
How to declare an interface?

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.

interface <interface_name>{

// declare constant fields


// declare methods that abstract
// by default.
} cgr 4
Interface

cgr 5
Interface

cgr 6
Interface

cgr 7
Interface

cgr 8
Interface
interface Drawable{
void draw();
}

class Rectangle implements Drawable{


public void draw(){System.out.println("drawing rectangle");}
}

class Circle implements Drawable{


public void draw(){System.out.println("drawing circle");}
}

class TestInterface1{
public static void main(String args[]){
Drawable d=new Circle();
getDrawable()
d.draw();
}} cgr 9
Interface

cgr 10
Interface
interface Printable{
void print();
}
interface Showable{
void show();
}
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();
}
} cgr 11
Interface
interface Printable{
void print();
}
interface Showable{
void show();
}
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();
}
} cgr 12

You might also like