Example 1: Java program for Abstract class
KW.java
abstract class Arithmetic
{
    int add(int a,int b)
    {
        int c=a+b;
        return c;
    }
    void display(int c)
    {
        System.out.println("Addition "+c);
    }
}

class KW extends Arithmetic
{
    public static void main(String[] args) 
    {
        KW k=new KW();
        int addition=k.add(10,10);
        k.display(addition);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Addition 20 kodingwindow@kw:~$
Example 2: Java program for Abstract class
KW.java
abstract class Fruit
{
    abstract void cutting();
}

class Mango extends Fruit
{
    void cutting()
    {
        System.out.println("Cutting Mango");
    }
}

class Apple extends Fruit
{
    void cutting()
    {
        System.out.println("Cutting Apple");
    }
}

class KW
{
    public static void main(String[] args) 
    {
        Mango m=new Mango();
        m.cutting();
        Apple a=new Apple();
        a.cutting();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Cutting Fruit Mango Cutting Fruit Apple kodingwindow@kw:~$
Example 3: Java program for Abstract class
KW.java
abstract class Shape
{
    abstract void display();
    abstract void area(float b,float h);
}

class Triangle extends Shape
{
    void display() 
    {
        System.out.print("Area of Triangle ");
    }
    void area(float b, float h) 
    {
        System.out.println((b*h)/2);
    }
}

class Rectangle extends Shape
{
    void display() 
    {
        System.out.print("Area of Rectangle ");
    }
    void area(float b, float h) 
    {
        System.out.println(b*h);
    }
}

class KW
{
    KW(float a)
    {
        System.out.println("Area of Square "+(a*a));
    }

    public static void main(String[] args) 
    {
        Triangle t=new Triangle();
        t.display();
        t.area(10,20);
        Rectangle r=new Rectangle();
        r.display();
        r.area(10,20);
        KW k=new KW(10);
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW Area of Triangle 100.0 Area of Rectangle 200.0 Area of Square 100.0 kodingwindow@kw:~$
Example 4: Java program for Abstract class
KW.java
abstract class Server
{
    String name;
    Server(String name)
    { 
        this.name = name; 
    }
    public abstract void start();
}

class Mongodb extends Server
{
    Mongodb(String name)
    {
        super(name);
    }
    public void start() 
    { 
        System.out.println( this.name + " Started Successfully"); 
    }
}

class KW
{
    public static void main(String[] args) 
    {
        Server s=new Mongodb("MongoDB Server");
        s.start();
    }
}
Output
kodingwindow@kw:~$ javac KW.java
kodingwindow@kw:~$ java KW MongoDB Server Started Successfully kodingwindow@kw:~$
Advertisement