[go: up one dir, main page]

0% found this document useful (0 votes)
12 views11 pages

interfaces

The document explains the concept of interfaces in programming, highlighting their role as blueprints for classes and their characteristics such as supporting abstraction and multiple inheritance. It details the differences between classes and interfaces, how to define and implement interfaces, and the use of variables within them. Additionally, it covers extending interfaces and provides examples of multiple inheritance through interfaces.

Uploaded by

gyashika198
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)
12 views11 pages

interfaces

The document explains the concept of interfaces in programming, highlighting their role as blueprints for classes and their characteristics such as supporting abstraction and multiple inheritance. It details the differences between classes and interfaces, how to define and implement interfaces, and the use of variables within them. Additionally, it covers extending interfaces and provides examples of multiple inheritance through interfaces.

Uploaded by

gyashika198
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/ 11

Topics to cover

Interfaces: Differences between classes and


interfaces, defining an interface,
implementing interface, applying interfaces,
variables in interface and extending
interface
What is an interface?
• Interface is a blueprint for a class. It is a completely
abstract class that is used to group related methods
with empty bodies.
• By default, variables in an interface are public, static
and final and methods are public and abstract.
• Used for achieving abstraction and multiple
inheritance.
• It can contain abstract methods, static methods,
default methods and static constant variables.
DIFFERENCE BETWEEN CLASS AND INTERFACE

CLASS INTERFACE
Variables can be instantiated and objects Objects cannot be created.
can be created. Variables can be initialized.
Class contains concrete methods(with Interface can contain only abstract,default
implementation ) . and static methods.
Access specifiers used: private, protected Only access specifier used is: public
and public.
Does not support multiple inheritance. Supports multiple inheritance.
extends • class->class

implements • class->interface

extends • interface->interface
Defining an interface
• interface keyword is used
• Example
import java.lang.* ;
public interface Animal
{
//final, static fields
int legs=4;
//abstract method declarations
void eat();
}
Implementing interfaces
• The class must implement all methods in an interface.
• Example
interface Animal{
void eat();
}
public class Mammal implements Animal {
public void eat() {
System.out.println(“Mammal eats”); }
}
class Main{
public static void main(String args[]) {
Mammal m= new Mammal();
m.eat();
}
}
OUTPUT: Mammal eats
Variables in interface
interface left{
int i=10;
}
interface right{
int j=100;
}
class test implements left, right {
public static void main(String args[]) {
System.out.println(left.i);
System.out.println(right.i);
}
}
OUTPUT: 10
100
Extending interfaces
• One interface can inherit another interface by using extends keyword.
interface Teacher {
void display1(); }
interface Student {
void display2(); }
interface T_S extends Teacher, Student {
void display3(); }
class college implements T_S {
public void display1() {
System.out.println(“Hi, I am teacher “); }
public void display2() {
System.out.println(“Hi, I am student “); }
public void display3() {
System.out.println(“Hi, I am teacher_student “); }
}
class Main
{
public static void main(String args[])
{
College c =new College();
c.display1();
c.display2();
c.display3();
}
}
OUTPUT: Hi, I am teacher
Hi, I am student
Hi, I am teacher_student
Multiple Inheritance
If a class extends multiple interfaces, or an interface extends multiple
interfaces, known as multiple inheritance.
interface A {
void display(); }
interface B {
void show(); }
class C implements A,B {
public void display(){
System.out.println(“Hello”); }
public void show(){
System.out.println(“World”); }
class Main {
public static void main(String args[]) {
C obj=new C();
obj.display();
obj.show();
}
}
Output: Hello
World

You might also like