Interface And Abstract class examples
Interface Examples
Simple example of implementing InterfaceSimple example of implementing Interface
interface Animal
{
void sound();
void eat();
}
class Dog implements Animal
{
public void sound().
{
System.out.println("dog Eats meat");
}
public void eat()
{
System.out.println("dog sound is Bark");
}
}
class Cat implements Animal
{
public void sound()
{
System.out.println("Cat drink milk");
}
public void eat()
{
System.out.println("Cat sound is meow");
}
}
public class Main
{
public static void main(String[] args)
{
Dog dog=new Dog();
dog.eat();
dog.sound();
Cat cat=new Cat();
cat.eat();
cat.sound();
}
}
Output
dog sound is Bark dog Eats meat Cat sound is meow Cat drink milk
Example to show extend keyword with interfaces
Example to show extend keyword with interfaces
interface shapeA
{
public String baseclass="shape";
public void Draw();
}
interface shapeB extends shapeA
{
public String baseclass="shape2";
public void Draw2();
}
class circle implements shapeB
{
public String baseclass="shape3";
public void Draw()
{
System.out.println("Drawing Circle here:"+baseclass);
}
public void Draw2()
{
System.out.println("Drawing Circle here:"+baseclass);
}
}
public class Main
{
public static void main(String[] args)
{
shapeA circleshape=new circle();
circleshape.Draw();
circleshape.Draw();
}
}
Output
Drawing Circle here:shape3 Drawing Circle here:shape3
Example to show class implementing more than one interfaces
Example to show class implementing more than one interfaces
interface Interface1
{
public void interface1Method();
}
interface Interface2
{
public void interface2Method();
}
class MyClass implements Interface1, Interface2
{
// Class definition including methods from both interfaces...
public void interface1Method()
{
System.out.println("First Interface method implementation");
}
public void interface2Method()
{
System.out.println("Second Interface method implementation");
}
}
public class Demo
{
public static void main(String[] a)
{
MyClass cls = new MyClass();
cls.interface1Method();
cls.interface2Method();
}
}
Output
First Interface method implementation Second Interface method implementation
Abstract class Examples
Abstract class exampleAbstract class example
abstract class Account
{
public void deposit()
{
System.out.println("this is deposit method");
}
public void withdraw()
{
System.out.println("this is withdraw method");
}
public void checkBalance()
{
System.out.println("this is check balance method");
}
public abstract float rateOfInterest();
}
class SavingAccount extends Account
{
public float rateOfInterest()
{
return 5;
}
}
class CurrentAccount extends Account
{
public float rateOfInterest()
{
return 3;
}
}
public class Demo
{
public static void main(String[] args)
{
SavingAccount savingAccount=new SavingAccount();
savingAccount.deposit();
savingAccount.withdraw();
savingAccount.checkBalance();
savingAccount.rateOfInterest();
CurrentAccount currentAccount=new CurrentAccount();
currentAccount.deposit();
currentAccount.withdraw();
currentAccount.checkBalance();
currentAccount.rateOfInterest();
}
}
Output
this is deposit method this is withdraw method this is check balance method this is deposit method this is withdraw method this is check balance method
Abstract class with interface examples
Abstract class with interface examples
interface Machine
{
void start();
void run();
}
abstract class Vehicle implements Machine
{
public void start()
{
System.out.println("Vehicle starting...");
}
public void run()
{
System.out.println("Vehicle running...");
}
}
class TowWheeler extends Vehicle
{
public void start()
{
System.out.println("TowWheeler starting...");
}
public void run()
{
System.out.println("TowWheeler running...");
}
}
class ThreeWheeler extends Vehicle
{
public void start()
{
System.out.println("ThreeWheeler starting...");
}
public void run()
{
System.out.println("ThreeWheeler running...");
}
}
public class Demo
{
public static void main(String[] args)
{
TowWheeler towWheeler=new TowWheeler();
towWheeler.start();
towWheeler.run();
ThreeWheeler threeWheeler=new ThreeWheeler();
threeWheeler.start();
threeWheeler.run();
}
}
Output
TowWheeler starting... TowWheeler running... ThreeWheeler starting... ThreeWheeler running...


