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

P6 Interfaces

Uploaded by

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

P6 Interfaces

Uploaded by

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

Programming

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)
}

// Pig "implements" the Animal interface


class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
public void sleep() {
// The body of sleep() is provided here
System.out.println("Zzz");
}
}
class Main {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
• Like abstract classes, interfaces cannot be used to
create objects (in the example above, it is not possible
to create an "Animal" object in the MyMainClass)
• Interface methods do not have a body - the body is
provided by the "implement" class
• On implementation of an interface, you must override
all of its methods
• Interface methods are by default abstract and public
• Interface attributes are by default public, static and
final
• An interface cannot contain a constructor (as it cannot
be used to create objects)
Why And When To Use
Interfaces?
• To achieve security - hide certain details and only
show the important details of an object (interface).

• Java does not support "multiple inheritance" (a


class can only inherit from one superclass).
However, it can be achieved with interfaces,
because the class can implement multiple
interfaces. Note: To implement multiple interfaces,
separate them with a comma (see example below).
interface FirstInterface {
public void myMethod(); // interface method
}

interface SecondInterface {
public void myOtherMethod(); // interface method
}

class DemoClass implements FirstInterface, SecondInterface {


public void myMethod() {
System.out.println("Some text..");
}
public void myOtherMethod() {
System.out.println("Some other text...");
}
}
class Main {
public static void main(String[] args) {
DemoClass myObj = new DemoClass();
myObj.myMethod();
myObj.myOtherMethod();
}
}
• An abstract class is a class that is declared
abstract —it may or may not include abstract
methods. Abstract classes cannot be
instantiated, but they can be subclassed.
• Abstraction is a process of hiding the
implementation details and showing only
functionality to the user.
• There are two ways to achieve abstraction in
java
1.Abstract class (0 to 100%)
2.Interface (100%)
• A class which is declared as abstract is known as
an abstract class. It can have abstract and non-
abstract methods. It needs to be extended and its
method implemented. It cannot be instantiated.
• Points to Remember
• An abstract class must be declared with an
abstract keyword.
• It can have abstract and non-abstract methods.
• It cannot be instantiated.
• It can have constructors and static methods also.
• It can have final methods which will force the
subclass not to change the body of the method.
Program
abstract class Bank{
abstract int getRateOfInterest();
}
class SBI extends Bank{
int getRateOfInterest(){return 7;}
}
class PNB extends Bank{
int getRateOfInterest(){return 8;}
}

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();
}

abstract class B implements A{


public void c(){System.out.println("I am c");}
}
class M extends B{
public void a(){System.out.println("I am a");}
public void b(){System.out.println("I am b");}
public void d(){System.out.println("I am 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();
}

class ABank extends Bank{


public void getBalance(){
System.out.println("$100 are Deposited in the
Bank");
}
}
class BBank extends Bank{
public void getBalance(){
System.out.println("$150 are Deposited in the Bank");
}
}

class CBank extends Bank{


public void getBalance(){
System.out.println("$200 are Deposited in the Bank");
}
}
public class Demo6{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Press 1 for Bank A");
System.out.println("Press 2 for Bank B");
System.out.println("Press 3 for Bank C");
int n = sc.nextInt();
Bank a = new ABank();
Bank b = new BBank();
Bank c = new CBank();
switch(n){
case 1:
a.getBalance();
break;

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

You might also like