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

(Lec-24)Java SE(Interfaces)

Interface
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

(Lec-24)Java SE(Interfaces)

Interface
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

JAVA SE

(CORE JAVA)
LECTURE -24
Today’s Agenda

• Interfaces in Java

• Creating and implementing Interfaces


Interface

• An interface is an alternate to pure abstract class i.e.


to support run time polymorphism.

• An interface is also a kind of Java class but with some


predefined restrictions which are as follows
1. An interface can contain data members but they will
by default be public, static and final by nature.
2. An interface can contain methods but they will be
public and abstract in nature.
3. Even if we don’t assign any visibility mode to the
members of an interface, still their visibility always
remains public and a programmer is not allowed to
alter it.
Interface

Syntax:-
interface <interface name>
{
<data type> <variable>= value;
<return type> <method name>(argument);
--------
--------
--------
}
class A implements <interface name>
{
-------
-------
}
Using interface as
Global Constants
import java.util.*; return kg*KG_TO_G;
interface Conversions }
{ public double
double KG_TO_G=1000; inch_to_mm(double inches)
double INCH_TO_MM=25.4; {
double kg_to_grams(double kg); return
double inches_to_mm(double inches*INCHES_TO_MM;
inches); }
} }
class TryConversions
implements Conversions
{
public double kg_to_gm(double
kg)
{
Using interface as
Global Constants

class TryConversions implements Conversions


{
public double kg_to_grams(double kg)
{
return kg*KG_TO_G;
}
public double inches_to_mm(double inches)
{
return inches*INCHES_TO_MM;
}
}
Using interface as
Global Constants
class TestConversions
{
public static void main(String [ ] args)
{
Scanner sc=new Scanner(System.in);
System.out.println(“Enter your weight in kg and height in inch”);
double wt=sc.nextDouble();
double ht=sc.nextDouble();
Conversions co=new TryConversions( );
System.out.println(“Converting your weight to grams
“+co.kg_to_gm(wt));
System.out.println(“After converting your height to mm
“+co.inches_to_mm(ht));
}
}
Interface

• A class can inherit only a single class at a time but a


class can implement multiple interfaces.
• Example:-
interface Point class Circle implements Point,
{ Shape
------ {
} ------
interface Shape ------
{ ------
------ ------
} }
What’s new in interface???

• Java 8 onwards a special feature is added in interface


which allows programmers to define methods in an
interface.

• Such methods which are defined in an interface are known


as default methods.

• It helps in situation where a class which implements an


interface might not have any logic to override a method
but has to forcefully override it with a blank body or just
return any default value.

• In this case any class which implements an interface is


exempted from overriding default methods.
Interface with
Runtime Polymorphism

• Just like we can use an abstract class to achieve


dynamic method dispatch, similarly we can use an
interface also to achieve polymorphic behavior .

• The reference of an interface can point to the


objects of its implementation class and can call
those methods which were declared in the interface
and have been overridden by the class.

• Let us try the Shape example which we did using


abstract classes.
Interface with
Runtime Polymorphism

interface Shape class Rectangle implements Shape


{
{ private double l,b;
double area( ); public Rectangle(double l,double b)
String getName( ); {
this.l=l;
} this.b=b;
}
public double area()
{
return l*b;
}
public String getName()
{
return "Rectangle";
}
Solution

class Triangle implements Shape


{
private double b,h;
public Triangle(double b,double h)
{
this.b=b;
this.h=h;
}
public double area()
{
return 0.5*b*h;
}
public String getName()
{
return "Triangle";
}
Solution

class UseShape
{
public static void main(String [ ] args)
{
Shape s;
s=new Rectangle(10,20);
S.o.p(“Shape is ”+s.getName());
S.o.p(“Its area is ”+s.area());
s=new Triangle(5,10);
S.o.p(“Shape is ”+s.getName());
S.o.p(“Its area is ”+s.area());
}
}
Inheriting one interface
into another
• Just like we inherit a class into another similarly, we
can inherit an interface into another.

• Though, a class cannot extend more then one class


at a time but an interface can extend many
interfaces at a time.

• An interface cannot implement any other interface.

• A .class file is created after compilation for every


interface.
Inheriting one interface
into another
• Example:-
interface Shape
{
double area();
}
interface Figure
{
String name();
}
interface MyShape implements Shape, Figure
{
----
}
End Of Lecture 24

For any queries mail us @: [email protected]


Call us @ : 0755-4271659, 7879165533

Agenda for Next Lecture:


1. Packages
2. Running programs outside bin
3. Packages and Visibility modes

You might also like