Beruflich Dokumente
Kultur Dokumente
Interfaces
By
Arvind Kumar
Asst. Professor, LPU
Interfaces
Interfaces
• An interface is a collection of abstract methods.
• Unless the class that implements the interface is abstract, all the
methods of the interface need to be defined in the class.
Properties of Interfaces
• The interface keyword is used to declare an interface.
interface Animal
{
public void eat();
public void travel();
}
Example
public class Mammal implements Animal
{
public void eat()
{
System.out.println("Mammal eats");
}
public void travel()
{
System.out.println("Mammal travels");
}
public int noOfLegs()
{ return 0; }
public static void main(String args[])
{
Animal m = new Mammal();
m.eat();
m.travel();
}
}
Abstract Class Vs Interfaces
Abstract Class Interface
Problem3: We can’t use abstract class here because it does not support
multiple inheritance.
Why Interfaces?
• We have Abstract classes…then what is the need of
Interfaces?
class Invoker
{
public static void invoke(Showable x)
{
x.show();
}
}
class A implements Showable
{
String name;
public A(String s)
{
name=s;
}
public void show()
{
System.out.println(“It is object:” + name);
}
}
class B implements Showable
{
int p;
public B(int x)
{
p=x;
}
public void show()
{
System.out.println(“value of p is:” + p);
}
}
Class InterfaceTest
{
public static void main(String arr[])
{
A m = new A(“Hello”);
B n = new B(10);
Invoker.invoke(m);
Invoker.invoke(n);
}
}
Default Method
• Java 8 introduces “Default Method” or (Defender methods)
new feature, which allows developer to add new methods to
the interfaces without breaking the existing implementation of
these interface.
}
Brain Storming Question
• interface Movable {
• public abstract void m1(); // line 1
• void m2(); // line 2
• public void m3(); // line 3
• abstract void m4(); // line 4
• }
• class Chair implements Movable { // line 5
• public void m1() { } // line 6
• void m2() { } // line 7
• }
• choose all the answers that apply:
• A) mark class Chair "abstract"
• B) mark Chair "abstract" and mark m2() "public"
• C) implement m3() and m4() in Chair (with public access modifier)
• D) implement the methods m3() and m4() in Chair (with public access
modifier) and mark m2() in Chair "public“
• Ans-BD
Let’s Do It
• Create a interface Shape with abstract method area. Then
create two subclasses of Shape named as Square and
Rectangle. Write a test program to calculate area of Square
and Rectangle by using same reference variable of Shape.
Let’s Do It
• Create an abstract class named Book . Include a String field
for the book’ s title and a double field for the book ’s price.
Within the class, include a constructor that requires the book
title, and add two get methods —one that returns the title and
one that returns the price. Include an abstract method named
setPrice(). Create two child classes of Book : Fiction and
NonFiction . Each must include a setPrice() method that sets
the price for all Programming Books to $24.99 and for all
Math’s Books to $37.99. Write a constructor for each subclass,
and include a call to setPrice() within each. Write an
application demonstrating that you can create both a
Programming and a Math’s Book and display their fields.
Let’s Do It
• Create a interface SavingAccount with data member
numberOfTransaction (4) and methods deposit() & withdraw().
• Create interface CurrentAccount with data member
minimumAmount and methods deposit() & withdraw().
• Create a class PNB which deals with Current Accounts and
Saving Accounts with necessary members and interest rate 4%.
• Create a class SBI which deals with Current Accounts and Saving
Accounts with necessary members and interest rate 5%.
• Write a user interactive program to handle the accounts of any
number of customers of saving and current account in PNB and
SBI.
Let’s Do It
• Write an application named BookArray in which you create an
array that holds 10 Books, some Programming and some
Math’s. Using a enhanced for loop, display details about all 10
books.
Let’s Do It
• Create an interface named Turner, with a single method named
turn().
• Create a class named Leaf that implements turn() to display
“Changing colors”.
• Create a class named Page that implements turn() to display
“Going to the next page”.
• Create a class named Pancake that implements turn() to
display “Flipping”.
• Write an application named DemoTurners that creates one
object of each of these class types and demonstrates the turn()
method for each class.
Let’s Do It
Let’s Do It
Let’s Do It