0% found this document useful (0 votes)
10 views4 pages

Interface

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Interface

interface printable{
void print();
}
class A implements printable{
public void print(){System.out.println("A");}
}

class Main
{
public static void main(String args[]){
A obj = new A();
obj.print();
}
}
interface Drawable{
void draw();
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}

class Circle implements Drawable{


public void draw(){System.out.println("drawing circle");}
}
class Main{
public static void main(String args[]){
Drawable d=new Circle();
d.draw();
}}

interface Bank{
float rateOfInterest();
}
class SBI implements Bank{
public float rateOfInterest(){return 9.15f;}
}
class PNB implements Bank{
public float rateOfInterest(){return 9.7f;}
}
class Main{
public static void main(String[] args){
Bank b=new SBI();
System.out.println("ROI: "+b.rateOfInterest());
}}

interface Printable{
void print();
}
interface Showable{
void show();
}
class A implements Printable,Showable{
public void print(){System.out.println("Hello");}
public void show(){System.out.println("Welcome");}
}

class Main
{
public static void main(String args[]){
A obj = new A();
obj.print();
obj.show();
}
}

// Since Java 8, we can have method body in interface.


// But we need to make it default or static method.

interface Drawable{
void draw();
default void msg(){System.out.println("default method");}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}

class Main{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
d.msg();
}
}
// Since Java 8, we can have method body in interface.
// But we need to make it default or static method.

interface Drawable{
void draw();
static int cube(int x){return x*x*x;}
}
class Rectangle implements Drawable{
public void draw(){System.out.println("drawing rectangle");}
}

class Main{
public static void main(String args[]){
Drawable d=new Rectangle();
d.draw();
System.out.println(Drawable.cube(3));
}}

//Creating interface that has 4 methods


interface A{
void a();//bydefault, public and abstract
void b();
void c();
void d();
}

//Creating abstract class that provides the implementation of one method of A interface
abstract class B implements A{
public void c(){System.out.println("I am C");}
}

//Creating subclass of abstract class, now we need to provide the implementation of rest of the
methods
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");}
}

//Creating a test class that calls the methods of A interface


class Test5{
public static void main(String args[]){
A a=new M();
a.a();
a.b();
a.c();
a.d();
}}
Abstract class
abstract class Bike{
abstract void run();
}
class Honda4 extends Bike{
void run(){System.out.println("running safely");}
public static void main(String args[]){
Bike obj = new Honda4();
obj.run();
}
}

abstract class Shape


{
abstract void draw();
}
class Rectangle extends Shape
{
void draw(){System.out.println("drawing rectangle");}
}
class Circle extends Shape
{
void draw(){System.out.println("drawing circle");}
}
class Main{
public static void main(String args[]){
Shape s=new Circle();//In a real scenario, object is provided through method, e.g., getShape()
method
s.draw();
}
}
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()+" %");
}}
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run();
void changeGear(){System.out.println("gear changed");}
}

class Honda extends Bike{


void run(){System.out.println("running safely..");}
}

class Main{
public static void main(String args[]){
Bike obj = new Honda();
obj.run();
obj.changeGear();
}
}

abstract class A {
static void A()
{
System.out.println("A");
}
}

public class Main extends A {


public static void main(String[] args)
{
A.A();
}
}

You might also like