P6 Interfaces
P6 Interfaces
Fundamentals II
Experiment 5: Interfaces
• An interface is a completely "abstract class" that is
used to group related methods with empty bodies.
// interface
interface Animal {
public void animalSound(); // interface method
(does not have a body)
public void run(); // interface method (does not
have a body)
}
To access the interface methods, the interface must
be "implemented" (kinda like inherited) by another
class with the implements keyword (instead of
extends). The body of the interface method is
provided by the "implement" class:
// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
public void sleep(); // interface method (does not have a body)
}
interface SecondInterface {
public void myOtherMethod(); // interface method
}
class TestBank{
public static void main(String args[]){
Bank b;
b=new SBI();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
b=new PNB();
System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");
}}
Program
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed")
;}
}
//Creating a Child class which inherits Abstract class
class Honda extends Bike{
void run(){System.out.println("running safely..");}
}
//Creating a Test class which calls abstract and n
on-abstract methods
class TestAbstraction2{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}
Program
interface A{
void a();
void b();
void c();
void d();
}
class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Program 1
1. We have to calculate the area of a rectangle, a square
and a circle. Create an abstract class 'Shape' with
three abstract methods namely 'RectangleArea' taking
two parameters, 'SquareArea' and 'CircleArea' taking
one parameter each. The parameters of
'RectangleArea' are its length and breadth, that of
'SquareArea' is its side and that of 'CircleArea' is its
radius. Now create another class 'Area' containing all
the three methods 'RectangleArea', 'SquareArea' and
'CircleArea' for printing the area of rectangle, square
and circle respectively. Create an object of class 'Area'
and call all the three methods.
abstract class Shapes
{
abstract public void Rectangle(float l,float b);
abstract public void Square(float s);
abstract public void Circle(double r);
}
class Area extends Shapes
{
public void Rectangle(float l,float b)
{
float A= l * b;
System.out.println("The area of rectangle is: " +
A);
}
public void Square(float s)
{
float B= s * s;
System.out.println("The area of Square is: " + B);
}
public void Circle(double r)
{
double C= 3.14 * r * r;
System.out.println("The area of Circle is: " + C);
}
}
class Demo4
{
public static void main(String args[])
{
Area a= new Area();
float l=15, b=20;
float s=20;
double r=7;
a.Rectangle(l,b);
a.Square(s);
a.Circle(r);
}
}
Program 2
Create an abstract class 'Bank' with an abstract
method 'getBalance'. $100, $150 and $200 are
deposited in banks A, B and C respectively. 'BankA',
'BankB' and 'BankC' are subclasses of class 'Bank',
each having a method named 'getBalance'. Call this
method by creating an object of each of the three
classes.
import java.util.Scanner;
abstract class Bank{
abstract public void getBalance();
}
case 2:
b.getBalance();
break;
case 3:
c.getBalance();
break;
default:
System.out.println("Wrong choice");
}
}
}
Program 3
We have to calculate the percentage of marks
obtained in three subjects (each out of 100) by
student A and in four subjects (each out of 100) by
student B. Create an abstract class 'Marks' with an
abstract method 'getPercentage'. It is inherited by
two other classes 'A' and 'B' each having a method
with the same name which returns the percentage of
the students. The constructor of student A takes the
marks in three subjects as its parameters and the
marks in four subjects as its parameters for student
B. Create an object for each of the two classes and
print the percentage of marks for both the students.
abstract class Marks{
abstract int getPercentage();
}
class A extends Marks{
int a,b,c;
A(int a, int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
int getPercentage(){
int average;
average = (a+b+c)/3;
return average;
}
}
class B extends Marks{
int a,b,c,d;
B(int a, int b, int c, int d){
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
int getPercentage(){
int average1;
average1 = (a+b+c+d)/4;
return average1;
}
}
class Demo9{
public static void main(String [] args){
A x = new A(96,99,92);
B y = new B(91,98,96,94);
System.out.println("Percentage of A is: ");
System.out.println(x.getPercentage());
System.out.println("Percentage of B is: ");
System.out.println(y.getPercentage());
}
}
THANK YOU
34